-->
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.  [ 3 posts ] 
Author Message
 Post subject: What is the key-many-to-one annotation equivalent
PostPosted: Fri Feb 01, 2008 1:43 pm 
Beginner
Beginner

Joined: Tue Jan 08, 2008 2:15 pm
Posts: 22
How can I have part of composite PK in a child assigned automatically by hibernate based on the pk of the parent (one field in the child = pk of the parent)?

Many messages were talking about key-many-to-one in the composite id declaration, but what is the annotation equivalent of key-many-to-one since I am not using mapping files?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 06, 2008 6:33 am 
Beginner
Beginner

Joined: Thu Jun 07, 2007 2:38 am
Posts: 28
Location: Italy, Rome
Did you had a look at this annotation? It is javax.persistence.PrimaryKeyJoinColumns


Code:
@PrimaryKeyJoinColumns({
        @PrimaryKeyJoinColumn(name="CUST_ID",
            referencedColumnName="ID"),
        @PrimaryKeyJoinColumn(name="CUST_TYPE",
            referencedColumnName="TYPE")
    })


You can put this as you key in your parent.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 06, 2008 9:26 am 
Beginner
Beginner

Joined: Tue Jan 08, 2008 2:15 pm
Posts: 22
Yest I did and I am using it on OneToOne. But my problem is with bidirectional OneToMany-ManyToOne using no association table. The child table has a FK to its parent in its PK.

I found two ways so far to work around that, but I am not sure which one is the best.

1) Map the association to the parent in the child as part of the PK in the child, not mapping the PK field itself. That seems to be the pure equivalent of a key-many-to-one in a composide-id tag. However, the Hibernate book says, on page 329 (second printing, dec 2006):

"The key-to-many-to-one construct also has limitations in queries: You can't restrict a query result in HQL or Criteria across a key-many-to-one join (although these features will be implemented in a later Hibernate version)."

And I am reluctant to use a mapping which will have those limitations (I am not sure if they are there in the current HB version - if a Hibernate team member could confirm, that would be great. It is also a pain to provide a parent entity when building the primary key class of the child.

2) Put additional code in my getter method for the primary key column which also has a bidirectional association to the parent (marked as insertable=false, updatable=false of course) - so when HB wants to get its value for cascade inserts, the code will obtain it from the parent since the bidirectional link to it is available and HB has pupulated its id field with a generated value.

That seems to do the job, but solution 1) is cleaner while solution 2) may not have the limitations of solution 1) regarding queries.

We all know surrogate keys are the way to go, but like many others, I have to work with what is there.


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