Same code not working same way in JSE as in JEE [message #1402961] |
Fri, 18 July 2014 15:21  |
Eclipse User |
|
|
|
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 #1402989 is a reply to message #1402986] |
Sat, 19 July 2014 05:29  |
Eclipse User |
|
|
|
So I am looking at some EclipseLink documentation from here:
http://wiki.eclipse.org/EclipseLink/FAQ/JPA#Do_I_have_to_maintain_both_sides_of_a_bi-directional_relationship.3F
When caching is on...
Here it clearly says that I should maintain both sides of the relation, in my case I guess this means that if I am adding a new Case than I have to take care of assign the home to the Case and add the Case to the home and merge both Case and Home.
What annoys me is that when I delete things I need to do a lot of cleaning code. But I am happy to do it if that is how it should be done (I was not sure before).
All I have to confirm now is if the different results in JSE and JEE is because on one caching is on and on the other is not. As I said I did set the flag to true for the JSE application and I still got queries sent to the database for each request.
Does someone know why?
[Updated on: Sat, 19 July 2014 06:28] by Moderator
|
|
|
Powered by
FUDForum. Page generated in 0.07570 seconds