首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Micronaut不可能使用PersistenceContext

Micronaut不可能使用PersistenceContext
EN

Stack Overflow用户
提问于 2019-10-15 14:10:15
回答 1查看 999关注 0票数 1

我是新来的,我试着跟着这个小项目。不过,我希望它能与postgres一起工作。

我的application.yml看起来是这样的:

代码语言:javascript
复制
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

我可以通过intellij访问数据库。在pom.xml中,我有以下依赖项:

代码语言:javascript
复制
       <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>

此外,在链接中提到需要annotionProcessor,因此我将其添加到构建概要文件中:

代码语言:javascript
复制
<annotationProcessorPaths>
    <path>
        <groupId>javax.persistence</groupId>
        <artifactId>javax.persistence-api</artifactId>
        <version>2.2</version>
    </path>
</annotationProcessorPaths>

所以每次我试着做以下几件事:

代码语言:javascript
复制
@PersistenceContext
private EntityManager entityManager;

我得到以下错误:

代码语言:javascript
复制
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).

但是,我已经启用了annotation processing。我还有一个@Entity-class:

代码语言:javascript
复制
@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;
    }
}

我在我的设计中到底遗漏了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-15 14:43:50

我的设置有很多问题。

  1. datasourcejpa需要在根级
  2. @Entity需要一个@Id

最后,application.yml看起来是这样的:

代码语言:javascript
复制
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

@Entity-Class需要一个@Id-Attribute:

代码语言:javascript
复制
@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 + '\'' +
                '}';
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58396675

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档