-->
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.  [ 4 posts ] 
Author Message
 Post subject: ManyToOne using join field and annotations?
PostPosted: Sat Aug 04, 2007 6:33 pm 
Newbie

Joined: Sat Aug 04, 2007 6:07 pm
Posts: 6
I think this may not be possible with the current JPA annotations, but just wanted to confirm.

I have a ManyToOne Child->Parent relationship that is not via a foreign key but instead via a shared identifier that happens to be unique on the Parent.

1) Is there any way to map this using the JPA annotations?

2) If not, is it possible to write some clean HQL to populate the Child object with the parent? e.g. I don't think a fetch join is applicable because the Child's ref would not be mapped. I guess I could use a projection and the constructor syntax...

Just wondering if there is a clean solution as this seems like a simple case.


thanks,
Pat Niemeyer


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 05, 2007 1:52 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
@JoinColumn(name = "MY_FK_COLUMN", referencedColumnName = "MY_UNIQUE_NONPK_COLUMN")

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 05, 2007 2:29 am 
Newbie

Joined: Sat Aug 04, 2007 6:07 pm
Posts: 6
Thanks for the response.

I had tried this earlier but was getting errors that I don't understand.

e.g. if Parent and Child each have a column called parentId:

@ManyToOne
@JoinColumn(name = "parentId", referencedColumnName = "parentId")
private Parent parent;

I then get a ClassCastException for the Parent class at org.hibernate.type.ManyToOneType.hydrate()

If I leave off the referencedColumnName. I get a different query error - Fail to convert to internal representation.

I have the Manning book and have been looking around elsewhere but cannot find any good documentation on the JoinColumn annotation or how to apply it.


Pat


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 05, 2007 12:41 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
You need to make the Parent class Serializable - that's why you're getting a class cast exception.

From the hibernate annotation documentation:
Quote:
Note that when using referencedColumnName to a non primary key column, the associated class has to be Serializable


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.