0

I am having a problem while starting my Spring Boot application:

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

The version of Spring Bot is 3.0 and Java is 17 (Most updated ones from Initialzr). I want to use H2 as in-memory database:

<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>

The package hierarchy is convenient to scan entities. So, I think it is not necessary to add @EntityScan (I tried it as well)

File structure

Here is 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

Repository

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

public interface CoffeeRepository extends JpaRepository<Coffee, Long>{

}

And the entity:

@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;
    }
}

I checked the similar problem in this thread, no any answer worked. I suspect about H2.

Thanks

I tried using @EntityScan and playing with application.properties. But still same error.

NecmiK
  • 3
  • 3
  • Why `javax.persistance` dependency is added explicitly? The `start-data-jap` will take care of that. Can you try to remove that dependency once. – Kunal Varpe Jan 04 '23 at 16:28

1 Answers1

0

I found the problem. Spring Boot 3.0.0 version started using jakarta instead of javax.persistence. Reorganizing the imports solved the problem.

NecmiK
  • 3
  • 3