调用EntityManager.persist()

时间:2019-04-15 21:39:04

标签: java jpa kotlin axon

我在QuoteProjection.java中有这堂课:

package com.mycompany.myapp.query;

import com.mycompany.myapp.command.domain.ProjectedQuote;
import javax.persistence.EntityManager;

public class QuoteProjection {

    private final EntityManager entityManager;
.
.
.
    public void on(CreateSubmissionEvt evt) {
        ProjectedQuote projectedQuote = new ProjectedQuote(evt.getAggregateId(), evt.getJobNumber());
        entityManager.persist(projectedQuote); // error reference this line
    }

我在ProjectedQuote中定义了myApi.kt

package com.mycompany.myapp.command.domain

@Entity
@NamedQueries(
        NamedQuery(name = "ProjectedQuote.fetch",
                query = "SELECT q FROM ProjectedQuote q WHERE q.aggregateId LIKE CONCAT(:idStartsWith, '%') ORDER BY q.id"),
        NamedQuery(name = "ProjectedQuote.count",
                query = "SELECT COUNT(q) FROM ProjectedQuote q WHERE q.aggregateId LIKE CONCAT(:idStartsWith, '%')")
)
data class ProjectedQuote(@Id var aggregateId: String, var jobNumber : String) { constructor() : this("", "") }

运行时,出现以下错误:

java.lang.IllegalArgumentException: Unknown entity:
com.mycompany.myapp.command.domain.ProjectedQuote
        at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:787) ~hibernate-core-5.2.17.Final.jar!/:5.2.17.Final]
        at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:768) ~[hibernate-core-5.2.17.Final.jar!/:5.2.17.Final]
.
.
.

根据下面的Lesiak的回答,我尝试将@EntityScan添加到应用程序Application.java中:

package com.mycompany.myapp.query;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;

@SpringBootApplication
@EntityScan(basePackages = {"com.mycompany.myapp.command.domain"})
public class Application {
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

...但是现在我收到以下错误,应用程序终止:

2019-04-16 15:34:54.265 ERROR 4212 --- [           main] o.s.boot.SpringApplication
          : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sagaStore' defined in class path resource [org/axonframework/springboot/autoconfig/JpaAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.axonframework.modelling.saga.repository.jpa.JpaSagaStore]: Factory method 'sagaStore' threw exception; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: SagaEntry is not mapped [SELECT new org.axonframework.modelling.saga.repository.jpa.SerializedSaga(se.serializedSaga, se.sagaType, se.revision) FROM SagaEntry se WHERE se.sagaId = :sagaId]
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:590) ~[spring-beans-5.0.9.RELEASE.jar!/:5.0.9.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1247) ~[spring-beans-5.0.9.RELEASE.jar!/:5.0.9.RELEASE]
.
.
.
错误消息中引用的

Sagas是Axon Framework的一部分。因此,通过对我的软件包执行@EntityScan,看起来好像是它导致它不扫描Axon软件包了吗?

我在头上。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

如何用@Entity注释课程

例如参见https://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/entity.html#entity-mapping-entity

更新

检查要扫描的实体。 https://springbootdev.com/2017/11/13/what-are-the-uses-of-entityscan-and-enablejparepositories-annotations/amp/

默认情况下,Spring Boot将启用实体扫描并查看@SpringBootApplication所在的包(及其子包)。如果您的配置在另一个包中包含实体,请使用@EntityScan

更新2

经过一番调查,事实证明这个问题是针对轴突的。

Axon需要注册自己的实体,例如更新问题中提到的SagaEntry。 它将这些实体注册在JpaAutoConfiguration中 为此,它使用自己的注释@RegisterDefaultEntities 不幸的是,如果您依靠默认的项目布局(域模型位于包含您的应用的软件包的子软件包中),则它们的配置效果很好, 但不能与@EntityScan

配合使用

检查问题https://github.com/AxonFramework/AxonFramework/issues/245

我的建议是重新打包您的应用程序(如果您的应用程序是软件包,则将其移至子软件包)并删除@EntityScan

如果您喜欢冒险,可以尝试扫描与使用其自定义注释扫描的软件包相同的软件包,但是我不确定这样做是否成功。