Search code examples
jpamicronaut

Micronaut - not possible to use PersistenceContext


I'am new to micronaut and I try to follow this little project. However I would like it to work with postgres.

My application.yml looks like this:

micronaut:
  application:
    name: hello-world
  datasources:
    default:
      url: 'jdbc:postgresql://localhost:5432/test'
      username: test
      password: test
      driver-class-name: org.postgresql.Driver
  jpa:
    default:
      properties:
        hibernate:
          hbm2ddl:
            auto: update
          show_sql: true

I have access to the database via intellij. In the pom.xml I have the following dependencies:

       <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.8</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>io.micronaut.configuration</groupId>
            <artifactId>micronaut-jdbc-hikari</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>io.micronaut.configuration</groupId>
            <artifactId>micronaut-hibernate-jpa</artifactId>
        </dependency>

Additionally it is mentioned in the link that one needs annotionProcessor, so I added this to my build profile:

<annotationProcessorPaths>
    <path>
        <groupId>javax.persistence</groupId>
        <artifactId>javax.persistence-api</artifactId>
        <version>2.2</version>
    </path>
</annotationProcessorPaths>

So now everytime I try to do the following:

@PersistenceContext
private EntityManager entityManager;

I get the following error:

Caused by: io.micronaut.context.exceptions.NoSuchBeanException: No bean of type [javax.persistence.EntityManager] exists. Make sure the bean is not disabled by bean requirements (enable trace logging for 'io.micronaut.context.condition' to check) and if the bean is enabled then ensure the class is declared a bean and annotation processing is enabled (for Java and Kotlin the 'micronaut-inject-java' dependency should be configured as an annotation processor).

However I already have annotation processing enabled. And I also have a @Entity-class:

@Entity
@Table(name = "users")
public class User {

    @NotBlank
    @Column(name = "name")
    private String userName;


    public User() {
    }


    public User(@NotBlank final String userName) {
        this.userName = userName;
    }


    public String getUserName() {
        return userName;
    }


    public void setUserName(final String userName) {
        this.userName = userName;
    }
}

What exactly am I missing in my setup?


Solution

  • There were multiple issues with my setup.

    1. datasource and jpa need to be at root level
    2. The @Entity needs a @Id

    So in the end the application.yml looks like this:

    micronaut:
      application:
        name: hello-world
    datasources:
      default:
        url: 'jdbc:postgresql://localhost:5432/test'
        username: test
        password: test
        driver-class-name: org.postgresql.Driver
    jpa:
      default:
        packages-to-scan:
          - 'test'
        properties:
          hibernate:
            hbm2ddl:
              auto: update
            show_sql: true
    

    And the @Entity-Class needs an @Id-Attribute:

    @Entity
    @Table(name = "users")
    public class User {
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
    
    
        @NotBlank
        @Column(name = "user_name")
        private String userName;
    
    
        public User() {
        }
    
    
        public User(@NotBlank final String userName) {
            this.userName = userName;
        }
    
    
        public String getUserName() {
            return userName;
        }
    
    
        public void setUserName(final String userName) {
            this.userName = userName;
        }
    
    
        @Override
        public String toString() {
            return "User{" +
                    "userName='" + userName + '\'' +
                    '}';
        }
    }