|Same code not working same way in JSE as in JEE [message #1402961]
||Fri, 18 July 2014 19:21
| Simon Joseph Aquilina
Registered: July 2014
I am posting this here as I am using GlassFish as an application server and to my knowledge this makes use of EclipseLink.
I have created an example with three entities; House, Officer and Case. A case is linked with one house and one officer. A house can have many cases and an officer can have many cases.
In other words; a House has at @OneToMany relation to Case and a Case has a @ManyToOne relation with House. Similar, an Officer has an @OneToMany relation to Case and a Case has a @ManyToOne relation with Officer.
Now, to be honest I did this example after working on a web project and not understand why JPA was acting in the way it did.
I create a House and persist this.
I create an Officer and persist this.
I create a Case and add the Officer and House to this, persist this.
When I load the House and see the number of Cases I find 0 cases.
When I load the Officer and see the number of Cases I find 0 cases.
This even though in the database I can see the Case table being populated!
If I re-start GlassFish, then, When I load the House and see the number of Cases I correctly find 1 case. If I load the Officer and see the number of Cases I correctly find 1 case.
I did search on the net any many answers I find all say the same thing, i.e. that - using the same example - when you add a House to a Case you must also add the Case to the House (and merge House) in order to be able to access same Case from House when re-loading the House for some other page.
Similarly, if I delete a Case I need to remove the Case from the House. Not doing so would mean that the House would still have a reference to the Case even though the case in the database has been deleted.
I wanted to see if this has to do with the the fact that this is a website. I therefore developed another example to run from console using same entities (same annotations, same underlying database).
Here things work as I expect them to work! i.e. - if I create a Case and assign a House and an Officer to this, after I persist this Case I can access the case from a House without the need manually assign the Case to the House and merge the House too. Similar if I delete a Case, when I re-load the House the said Case is no longer accessible from House instance.
I was wondering why does it work different in JSE and JEE? And am I doing something wrong in JEE to get the behavior I am getting?
Example code here: dl.dropboxusercontent.com/u/27542229/jpa-examples.zip
Powered by FUDForum
. Page generated in 0.02626 seconds