I’m trying to write JUnit tests for my company’s application. For some reason hibernate isn’t discovering my @Entity classes. The application works normally so I know the entity classes are annotated correctly.
I wrote this code in my unit test method
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("company.persistence.unit");
EntityManager entityManager = entityManagerFactory.createEntityManager();
User user = entityManager.createQuery("select u from User u", User.class).getSingleResult();
My entity classes are in src/main/java/my/company/package/models and my unit test code is in src/test. From what I’ve read I would expect that hibernate should just find the entity classes automatically, but when I run an HQL query I get an exception which says “org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [select u from User u]”. I put <class>
elements in the persistence.xml file for the User class (and all of the models that are referenced by User) and I don’t get that same error any more, instead I get a NullPointerException at Persistence.createEntityManagerFactory
. The first item in the stack trace is “org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processFkSecondPassesInOrder(InFlightMetadataCollectorImpl.java:1745)”
I also tried using the <jar-file>my-application.war</jar-file>
element but I still get “…User is not mapped…” exception. I don’t know if it being a war file makes any difference. Using the <jar-file>
would be preferrable since there are hundreds of entity classes in my company application that I would end up having to list in the persistence.xml file, and have to keep them updated too.