Jonathan M
Jonathan M

Reputation: 17441

Unknown Entity when calling EntityManager.persist()

I have this class in 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
    }

I've defined ProjectedQuote in 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("", "") }

When running, I get the following error:

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

Per Lesiak's answer below, I tried adding @EntityScan to the application, 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);
  }
}

...but now I get the following error and the application terminates:

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, referenced in the error message, are part of the Axon Framework. So by doing an @EntityScan on my packages, it looks like it caused it to NOT scan the Axon packages, maybe?

I'm in over my head. Any help is appreciated.

Upvotes: 5

Views: 4150

Answers (1)

Lesiak
Lesiak

Reputation: 25936

How about annotating your class with @Entity

See for example https://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/entity.html#entity-mapping-entity

Update

Check which entities are scanned. https://springbootdev.com/2017/11/13/what-are-the-uses-of-entityscan-and-enablejparepositories-annotations/amp/

By default, Spring Boot will enable entity scanning and look in the package (and its subpackages) where @SpringBootApplication is located. If your configuration has entities in another package, use @EntityScan

Update 2

After some investigation, it turned out that the question is very Axon-specific.

Axon needs to register its own entities, like SagaEntry mentioned in the updated question. It registers these entities in JpaAutoConfiguration To do so, it uses its own annotation @RegisterDefaultEntities Unfortunately their configuration works well if you rely on the default project layout (domain models in a sub-package of a package containing your app), but doesn't play nice with @EntityScan

Check issue https://github.com/AxonFramework/AxonFramework/issues/245

My advice is to repackage your application (move entitiees to a sub-package if your app package) and remove @EntityScan

If you are adventurous, you can try scaning the same packages that are scanned by their custom annotation, but I am not sure if this will succeed.

Upvotes: 4

Related Questions