1

I am currently developing a Java EE project using hibernate and JPA, and am facing an issue I really don't know how to handle. This is the layout of the project:

There is a module, lets call it module A, which has a persistence.xml and a lot of entities mapped to a database. I am about to add new functionality, and as the new functionality has its own responsibilities I have a new module, module B, with its own persistence.xml. Both module A and module B are deployed as ears in a JBoss.

The design of the database (which is final and can't be changed), requires one of the entities in module B to have a foreign key in module A. In B's persistence.xml I added <class>-tags to the foreign key so that hibernate can find it. The foreign key entity class has itself references to other entities, so I had to add those to the persistence.xml as well. My problem is that one of those entities has a constructor expression jpql query, selecting data from an entity into a usual POJO. In module B when hibernate tries to resolve all the <class>-tags it complains that the JPQL query is invalid (as it does not know of the POJO). Is there a way to make the persistence unit in module B aware of this object? I would really like to leave all classes in module A untouched as they are sort of "legacy code", which I know already works (chnaging them might break the structure of module A).

2
  • Maybe declaring all module A classes on module B's persistence.xml would do the trick?
    – Bonifacio
    Feb 10, 2016 at 18:11
  • @Bonifacio Yes, I hope so. However I think that even though specifying all classes from module A in B's persistence.xml hibernate will still only look at entities. I thought about adding the classes through a <jar-file> dependency but the jar is inside an ear, and the name is specified as "module-a-version-minorVersion.jar".
    – Pphoenix
    Feb 10, 2016 at 19:20

1 Answer 1

0

I finally found the problem, here is my fix.

When I specifically added the classes from module A to module B with the <class>-tag I accidentally added a super class, which contained the query that hibernate thought was erroneous. In fact, the query was erroneous if only looking at that super class since one of the variables was only available in the sub classes (the query looks like this: select new MyPojoClass(x.id, x.var1, x.var2, x.variableAvailableInSubClass) from MyTable x). To solve the problem I instead added all the sub classes with <class>, making hibernate able to resolve all dependencies and queries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.