0

I have base entity

@MappedSuperclass
@EntityListeners({AuditingEntityListener.class})
public class BaseAuditEntity extends BaseEntity {
      ...
} 

and the entity below

package core.entities
@Entity
public class Customer extends BaseAuditEntity {
    ...
}

when I try to query database using criteria builder like below:

CriteriaQuery<BaseIdCodeDto> query =
    getCriteriaBuilder().createQuery(BaseIdCodeDto.class);

Root<Customer> root = query.from(Customer.class);

it says:

org.springframework.dao.InvalidDataAccessApiUsageException: Not an
 entity: class core.entities.Customer; nested exception is
 java.lang.IllegalArgumentException: Not an entity:

I dont have a persistence.xml file. I know that other projects are working without this file so how can I fix this? What is the problem?

Customer class is located in another project. I add maven dependency to the pom of the current project. The problem could be this, but I have no solution.

I created Customer class in my current project. The error changed. This time it does not see one to one depended entities like CustomerChannel. This class is also located in the depended project and also annotated with @Entity as well. So why doesn't spring boot see it? I expect all the classes annotated with the @Entity should be registered as entities, but spring boot does not register entities in the depended projects. Am I wrong to expect this?

It says

Customer.customerChannel references an unknown entity: 
core.entities.CustomerChannel

It looks like if I add all of the entities from the depended project to the current project, the problem will be fixed but I cant do it because there are multiple projects using the same entities.

Any help would be appreciated.

ylmzzsy
  • 7
  • 2
  • seems like you have imported the class wrong - make sure that you are importing your `Customer` class and not from some library – J Asgarov Jul 05 '23 at 14:06
  • I have checked the package, it seems to be correct – ylmzzsy Jul 05 '23 at 14:11
  • and what is the import of the `@Entity` annotation? – J Asgarov Jul 05 '23 at 18:28
  • Try to add annotation `@EntityScan( basePackages = {"your package - core.entities"} )` to your `ServerApplication` class – D.T Jul 06 '23 at 04:54
  • what do you mean by `ServerApplication` class? is it @SpringBootApplication annotated class? @D.T – ylmzzsy Jul 06 '23 at 05:52
  • @D.T thanks dude you comment fixed the issue, why don't you post it as an answer? However, my other projects does not have `@EntityScan` tag, but they are still working. Is there any other way to fix the issue? – ylmzzsy Jul 06 '23 at 06:41
  • @ylmzzsy in that case I think your class package are not really in order, could you update the post with your hierarchy in your project? – D.T Jul 06 '23 at 09:57

0 Answers0