【问题标题】:Spring Boot Entity and Repository Not Working Without AnnotationSpring Boot 实体和存储库在没有注释的情况下无法工作
【发布时间】:2022-11-24 14:09:27
【问题描述】:

我读了这篇 stackoverflow 帖子,上面说我们不需要使用 @EnableJpaRepository@EntityScan Spring data repository works without annotations

我不确定出了什么问题。但是我的 Spring Boot 应用程序只能以 @EnableJpaRepository@ComponentScan 开头。

我的应用程序.java

package com.domain.spring.example;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@ComponentScan("com.domain.spring.example.repository")
@EnableJpaRepositories(
    basePackages = "com.domain.spring.example.model",
    entityManagerFactoryRef = "entityManagerFactory"
)
public class MyApplication {

  public static void main(String[] args) {
    new SpringApplicationBuilder(MyApplication.class).run(args);
  }
}

项目.java

package com.domain.spring.example.model;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import java.math.BigDecimal;
import java.util.UUID;

import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;
import org.hibernate.annotations.GenericGenerator;

@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
@Table(name = "items")
public class Item {

  @Id
  @GeneratedValue(generator = "UUID")
  @GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
  private UUID id;

  private String name;

  private BigDecimal price;
}

ItemRepository.java

package com.domain.spring.example.respository;

import com.domain.spring.example.model.Item;
import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ItemRepository extends JpaRepository<Item, UUID> {

}

项目服务.java

package com.domain.spring.example.service;

import com.domain.spring.example.respository.ItemRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class ItemService {

  private final ItemRepository itemRepository;


}

构建.gradle

plugins {
    id 'java'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'org.springframework.boot' version '2.6.4'
}

group 'com.domain'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

configurations {
    testIntegrationImplementation.extendsFrom implementation
    testIntegrationRuntimeOnly.extendsFrom runtimeOnly
}

dependencies {

    annotationProcessor group: 'org.hibernate', name: 'hibernate-jpamodelgen'
    annotationProcessor group: 'org.projectlombok', name: 'lombok', version: '1.18.22'
    testAnnotationProcessor group: 'org.projectlombok', name: 'lombok', version: '1.18.22'

    implementation group: 'com.vladmihalcea', name: 'hibernate-types-52', version: '2.14.0'
    implementation group: 'jakarta.persistence', name: 'jakarta.persistence-api', version: '3.1.0-RC2'
    implementation group: 'javax.persistence', name: 'javax.persistence-api', version: '2.2'
    implementation group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final'
    implementation group: 'org.hibernate', name: 'hibernate-core'
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa'
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-webflux'
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-validation'
    implementation group: 'org.springframework.cloud', name: 'spring-cloud-starter-netflix-eureka-client', version: '3.1.1'
//    implementation group: 'org.springframework.data', name: 'spring-data-jpa'
    implementation group: 'org.springframework.data', name: 'spring-data-redis'
    implementation group: 'org.projectlombok', name: 'lombok', version: '1.18.22'
    implementation group: 'org.postgresql', name: 'postgresql'

    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
    testImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-test'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0'

    testIntegrationImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-test'
}

test {
    useJUnitPlatform()
}

应用程序.properties

# Eureka
eureka.client.enabled=false
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=true
eureka.client.service-url.defaultZone=http://localhost:4001/eureka/
eureka.instance.prefer-ip-address=true

spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost/example
spring.datasource.username=postgres
spring.datasource.password=123
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.check_nullability=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.jdbc.batch_size=100
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false

# Logging
logging.level.org.springframework.web=DEBUG
logging.level.org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler=OFF
logging.level.org.springframework.web.HttpLogging=OFF

MyApplication.java 中没有 @EnableJpaRepositories 注释。我收到以下错误

Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.domain.spring.example.model.Item

MyApplication.java 中没有 @ComponenctScan 注释。我收到以下错误

Parameter 0 of constructor in com.domain.spring.example.service.ItemService required a bean of type 'com.domain.spring.example.respository.ItemRepository' that could not be found.

此外,ddl-auto=create 属性也不起作用。 如何修复它以便我可以在不添加注释的情况下使用默认的 Spring JPA?

更新: 我认为可能有任何问题阻止 Spring 正确扫描存储库、实体和路由。当我点击 GET /v1/test 时,这个简单的路由返回 404

package com.domain.spring.example.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("/v1")
@RequiredArgsConstructor
public class MyController {

  @GetMapping("/test")
  public Mono<String> test() {
    return Mono.just("test");
  }
}

【问题讨论】:

  • 您的 SpringApplication 类似乎缺少指向您的 Item 类的 @EntityScan
  • 存储库不应该用@Repository注释吗?
  • @Ascalonian 它不影响
  • @AnadiMisra 我认为没有必要。我的问题上的链接说不需要。在其他项目中,我从不使用@Repository并且它有效

标签: java spring spring-boot hibernate jpa


【解决方案1】:

您正在寻找此包中的存储库

@EnableJpaRepositories(
    basePackages = "com.domain.spring.example.model"

但是你的存储库在这个包中

com.domain.spring.example.respository

【讨论】:

    【解决方案2】:

    这里有两件事:

    @EnableJpaRepositories(
        basePackages = "com.domain.spring.example.respository",
        entityManagerFactoryRef = "entityManagerFactory"
    )
    

    此外,由于您使用的是 SpringBoot 应用程序,它的自动连接应该负责实例化存储库,前提是用 @SpringBootApplication 注释的类位于您拥有的顶级包中,所以我认为您也应该尝试不使用 @EnableJpaRepositories

    【讨论】:

    • 没有 @EnableJpaRepositories 注释,我仍然遇到同样的错误。错误是Not a managed type: class com.domain.spring.example.model.Item
    • 我更新了我的问题细节。似乎还有另一个问题,因为定义的路线也未被识别。
    • 这是我用的@RequiredArgsConstructor(onConstructor_ = {@Autowired})
    【解决方案3】:

    我遇到了同样的问题。就我而言,问题在于依赖性,我使用的是 spring 依赖性

    弹簧数据-jpa

    代替

    spring-boot-starter-data-jpa

    将 spring-data-jpa 更改为 spring-boot-starter-data-jpa 对我有用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-03
      • 2019-01-26
      • 1970-01-01
      • 2016-05-04
      • 2020-10-21
      • 2016-02-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多