File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes populating @OneToOne from query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "populating @OneToOne from query" Watch "populating @OneToOne from query" New topic
Author

populating @OneToOne from query

Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31079
    
163

I'm trying to have another entity populated when I load the main entity (Attachment.) This works just fine on my local machine. On the server, AttachmentInfo is null. I checked in the database that attach_id is the same in both tables. (I also confirmed if I write a query to get the AttachmentInfo for that id in JPA, it works just fine. What am I missing?





[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

Is there any exception when you start up your app? I could not find any error so far....O.o


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
Cesar Loachamin
Ranch Hand

Joined: Dec 25, 2010
Posts: 90

Hi Jeanne

Try executing the query within a transaction, or you must use a fetch join with the AttachmentInfo relationship.
Kind regards
Cesar


When a dream is ending because to come true - OCPJP 6,7. OCE JPA EE6. MCTS
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31079
    
163

No exception on startup. And I am inside a transaction.

It does work on the production server (just not the test one) so I wonder if something odd is going on there. I tried the fetch join and it didn't return null. But it also didn't behave as expected. The data looks fine on test, but now I wonder if there is something subtle going on...

In general should the fetch join be required if I have fetch set on the field?
Cesar Loachamin
Ranch Hand

Joined: Dec 25, 2010
Posts: 90

Hi Jeanne

In general should the fetch join be required if I have fetch set on the field?
The answer is no, if the relationship is a single value the default fetch type is eager.
I have two observations, first is about the query the postId attribute doesn't exist in the entity Attachment, and the other is about the way you mapping the relationship:

Kind regards
Cesar
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31079
    
163

Cesar Loachamin wrote:Hi Jeanne
I have two observations, first is about the query the postId attribute doesn't exist in the entity Attachment, and the other is about the way you mapping the relationship:

postId does exist on Attachment. I omitted it when posting because it was an extraneous detail. I wanted less code so the relationship parts would be more obvious. I'm not clear what your observation is on the relationship
Cesar Loachamin
Ranch Hand

Joined: Dec 25, 2010
Posts: 90

Ok I understand, About the relationship I use the @JoinColumn in the entity owner the relationship AttachmentInfo because this entity has the foreign key column, and in the Attachment I use the mappedBy property of the OneToOne annotation to indicate the inverse relationship.
Kind Regards
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31079
    
163

With that change, I get a null error locally when trying to retrieve the attachment object from the database. It complains the attach_id is null. Which it isn't.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10290
    
168

I was working on these same entities this weekend and this went smooth on my local instance. But just like you, I started running into problems on the test server where (in a slightly different flow) I was getting a null AttachmentInfo for an Attachment when actually the info really existed for that attachment. Spent almost an hour thinking of what's going wrong and it didn't help that I couldn't reproduce this locally. Finally, I stumbled upon your comment in the some other part of the code where you pointed to this thread about running into the same issue (thanks for adding that comment!)

So I decided to dig a bit more into the test server instance and see the difference. While doing so I enabled the logging of SQLs generated by Hibernate and turns out the SQL being generated is incorrect (on all instances - local, test and production):



Notice the join clause (attachment0_.attach_id=attachment1_.attach_desc_id) - it's completely wrong. The reason why this works locally and in production is just by chance since the keys generated for both those tables are identical. So locally and in production the attach_id matches the attach_desc_id in the other table. Unlike in test server instance where those ids are different.

I looked around some documentation and the mapping to understand why that incorrect join clause is being generated. Thanks to James' and Cameron's answers here, I was able to fix the JPA mapping on those classes. So now those classes have been fixed to:






and Hibernate now generates the correct query:





[My Blog] [JavaRanch Journal]
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31079
    
163

Wow. That's really interesting. Thank you for looking into it deeper and posting here!
 
wood burning stoves
 
subject: populating @OneToOne from query