【问题标题】:Spring boot with H2 - Not a managed type带有 H2 的 Spring boot - 不是托管类型
【发布时间】:2023-01-04 20:39:03
【问题描述】:

我在启动 Spring Boot 应用程序时遇到问题:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'coffeeService': Unsatisfied dependency expressed through field 'coffeeRepository': Error creating bean with name 'coffeeRepository' defined in com.coffeetime.coffeeshop.repository.CoffeeRepository defined in @EnableJpaRepositories declared on CoffeeshopApplication: Not a managed type: class com.coffeetime.coffeeshop.domain.Coffee
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'coffeeRepository' defined in com.coffeetime.coffeeshop.repository.CoffeeRepository defined in @EnableJpaRepositories declared on CoffeeshopApplication: Not a managed type: class com.coffeetime.coffeeshop.domain.Coffee
Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.coffeetime.coffeeshop.domain.Coffee

Spring Bot 的版本是 3.0,Java 是 17(最更新的来自 Initialzr)。 我想使用 H2 作为内存数据库:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>javax.persistence-api</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>

包层次结构便于扫描实体。所以,我认为没有必要添加@EntityScan(我也试过了)

File structure

这是 application.properties:

spring.datasource.url=jdbc:h2:mem:coffeeshopdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=pass1234
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

spring.jpa.show-sql=true
spring.h2.console.enabled=true

spring.jpa.defer-datasource-initialization=true

资料库

import org.springframework.data.jpa.repository.JpaRepository;
import com.coffeetime.coffeeshop.domain.Coffee;

public interface CoffeeRepository extends JpaRepository<Coffee, Long>{

}

和实体:

@Entity
@Table(name = "coffee")
public class Coffee {
    
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    
    @Column(name = "name")
    @NotEmpty(message = "Coffee name cannot be empty")
    private String name;
    
    @Column(name = "amount")
    @NotNull(message = "Coffee price cannot be empty")
    @Min(value = 0, message = "Coffee price must be greater than or equal to 0")
    private BigDecimal amount;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public BigDecimal getAmount() {
        return amount;
    }

    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }
}

我在这个线程中检查了the similar problem,没有任何答案有效。我怀疑H2。

谢谢

我尝试使用 @EntityScan 并使用 application.properties。但仍然是同样的错误。

【问题讨论】:

    标签: spring-boot spring-data-jpa spring-data h2


    【解决方案1】:

    我发现了问题。 Spring Boot 3.0.0 版本开始使用 jakarta 而不是 javax.persistence。重组进口解决了这个问题。

    【讨论】:

      猜你喜欢
      • 2015-04-24
      • 2019-12-30
      • 2022-08-07
      • 1970-01-01
      • 2017-01-20
      • 2019-02-07
      • 1970-01-01
      • 2018-02-25
      • 1970-01-01
      相关资源
      最近更新 更多