-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Access to key values from proxy on many to one ?
PostPosted: Thu Sep 29, 2011 12:20 pm 
Newbie

Joined: Wed Apr 28, 2010 11:55 am
Posts: 13
I have a many-to-one where the 'one' part has a key value, but no row in the DB.
I need to be able to access the key for this missing row, but I get an ObjectNotFoundException.

My understanding is that the proxy contains the key value and you can access that without causing DB access.

Parent Entity:
Code:
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "ORDER_ID")
  private MpuEntity mpu;


MpuEntity:
Code:
  @Id
  @Column(name = "ORDER_ID", unique = true, nullable = false)
  private Long orderId;


Issue (this throws a ObjectNotFoundException):
Code:
Long id = parent.getMpu().getOrderId();


Is there a way to get the key value when the child row does not exist ?

Thanks,
Craig.


Top
 Profile  
 
 Post subject: Re: Access to key values from proxy on many to one ?
PostPosted: Thu Sep 29, 2011 2:03 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
My guess is that you need to have the @Id annotation on the getter method instead of on the private field.


Top
 Profile  
 
 Post subject: Re: Access to key values from proxy on many to one ?
PostPosted: Thu Sep 29, 2011 3:17 pm 
Newbie

Joined: Wed Apr 28, 2010 11:55 am
Posts: 13
I believe that annotations may be specified on the variable declaration or the getter. It should make no difference to Hibernate.


Top
 Profile  
 
 Post subject: Re: Access to key values from proxy on many to one ?
PostPosted: Thu Sep 29, 2011 4:03 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Yes, there is a difference. Access to fields can't be intercepted by proxies. Hibernate can't really know that the getOrderId() method is related to the orderId field, so I think Hibernate then initializes the proxy on any method call. I remember seeing some information about it, but right now I can't find it again.


Top
 Profile  
 
 Post subject: Re: Access to key values from proxy on many to one ?
PostPosted: Thu Sep 29, 2011 5:49 pm 
Newbie

Joined: Wed Apr 28, 2010 11:55 am
Posts: 13
nordborg wrote:
Yes, there is a difference. Access to fields can't be intercepted by proxies. Hibernate can't really know that the getOrderId() method is related to the orderId field, so I think Hibernate then initializes the proxy on any method call. I remember seeing some information about it, but right now I can't find it again.



I found the problem. I had a named query that had 'parent.getChild().getField() = :x' in it and it was this that triggered the exception.

Tomorrow I will revert the code to what it was originally (I changed it to remove the many-to-one). If the key access does not work I'll owe you a beer :), but I suspect I will be the one enjoying the beverage.

Regards,
Craig.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.