• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

populating @OneToOne from query

 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?



 
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any exception when you start up your app? I could not find any error so far....O.o
 
Ranch Hand
Posts: 90
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeanne

Try executing the query within a transaction, or you must use a fetch join with the AttachmentInfo relationship.
Kind regards
Cesar
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 90
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 90
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:



 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow. That's really interesting. Thank you for looking into it deeper and posting here!
 
I'm so happy! And I wish to make this tiny ad happy too:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic