Same code not working same way in JSE as in JEE [message #1402961] |
Fri, 18 July 2014 19:21 |
Simon Joseph Aquilina Messages: 4 Registered: July 2014 |
Junior Member |
|
|
Hello,
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
|
|
|
Re: Same code not working same way in JSE as in JEE [message #1402986 is a reply to message #1402961] |
Sat, 19 July 2014 08:24 |
Simon Joseph Aquilina Messages: 4 Registered: July 2014 |
Junior Member |
|
|
Just to add that in JEE I managed to replicate the same results as in JSE example by turning caching off.
<property name="eclipselink.cache.shared.default" value="false"/>
That said, I turned caching on for the JSE example (I used same property but set value to "true"), however, in the logs I can see that sql queries are still being sent to the database for each request I make. This would explain why in JSE it is working and not in JEE (when caching is on).
[Updated on: Sat, 19 July 2014 09:00] Report message to a moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.03062 seconds