I've got a jar file of JPA entities; they are mapped primarily using annotations, but, since we need to use them against several databases, we map the primary key field for each entity in an orm.xml file (so the key generation strategy can be properly specified; nothing else changes between databases).
We've been using the entities successfully for quite some time in a Java SE application. Now, though, as I am attempting to deploy the entity jar in the lib directory of an ear file (and the JPA mapping files in the META-INF directory), I am getting a strange error upon deployment from entities with bi-directional relationships. For example, we have a Process entity and a ProcessType entity; a Process can have only 1 ProcessType, but a ProccessType can have many associated Processes, so we mapped the bi-directional relationhip between Process and ProcessType as follows:
@ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name="process_type_id", nullable=false) private ProcessType processType;
@OneToMany(mappedBy="processType", fetch=FetchType.LAZY) @JoinColumn(name="process_type_id", nullable=false) private Set<Process> processes;
As I mentioned, we have many relationships of this nature and have been using them without issue in a Java SE application for a couple of months now. When I attempt to deploy the entity jar (and mapping files) as part of an ear file (in JBoss 5.1.0.GA), I get the following error:
1:14:11,047 ERROR [AbstractKernelController] Error installing to Start: name=persistence.unit:unitName=opt_process_control.ear/#logos state=Create
org.hibernate.AnnotationException: @OneToOne or @ManyToOne on werner.opt.logos.config.Process.processType references an unknown entity: werner.opt.logos.global.ProcessType
This has me quite confused; I'm not even sure what I can test to narrow this down, since the exact same jar file works elsewhere, and the entities pass all their tests (there's a TestNG unit test for each entity, as well as a test for the bidirectional relationship between Process and ProcessType; all of the tests succeed). Please, please let me know if you see anything I've missed, or if you have any ideas for me to try. I truly appreciate your help!
Some additional information (that didn't prove very illuminating to me). If I change the mapping from the Process entity to the ProcessType entity to the following, the deployment error goes away (well, actually, it goes to the next relationship between entities of this type, and then causes the same exception...but it does seem to have some effect):
Process entity (alternate mapping):
@Column(name="process_type_id", nullable=false) private ProcessType processType;
I have no idea why the first way (which seems to be the encouraged way to handle such relationships) is not working, but this way does seem to work (or at least, gets past the current deployment error).
Just thought I should include this in case it helps anyone figure out what I might be doing wrong. I _really_ appreciate any help anyone might have...Thank you so much!
I figured out why the relationships in the deployed jar were not working, but I'm not sure if it is due to a bug in JBoss or if it is intentional. Basically, we have a jar file of entities which are mapped using annotations _except_ for the primary key generation strategy-- we are using the entities against multiple databases, so we put the id generation strategy for each entity in an orm.xml file. We have one schema, though, where the entities are _completely_ mapped using only annotations, so the entities for that schema do not appear in the orm.xml file. When we use this setup in a JavaSE application, it works fine-- the entities are discovered and mapped using annotations, and the orm.xml file provides the primary key generation strategy for those entities that need it. When I deployed the jar and orm.xml file as part of an ear file in JBoss, however, it failed to find any entities that were not in the orm.xml file (the entities that were completely mapped using annotations, and thus did not need to be in the orm.xml file). Once I added these entities to the orm.xml file, everything worked as expected-- JBoss found the entities that were "missing" and keeping the bidirectional relationships from working.
Is this the expected behavior with JBoss? If an orm.xml file is used, do _all_ entities need to be in it, even if they are completely mapped using annotations? Why does this behavior differ from using hibernate/JPA in a JavaSE environment?
Although I was able to get around the issue I was having, I would really like to understand why it was happening in the first place. I would really appreciate any help answering these questions...the difference in behavior between JavaSE and JavaEE environments is troubling.
Thanks in advance,