-->
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.  [ 6 posts ] 
Author Message
 Post subject: How to specify FK that is not PK of other table
PostPosted: Sun Jan 15, 2006 12:44 am 
Newbie

Joined: Sun Jan 15, 2006 12:28 am
Posts: 4
I can't figure out how to do something.

I have one table named "authority" that has Id, username, and privilege.

I have another table named "user_account" that has Id, and username.

I want to be able to just add Authority objects to the UserAccount object and have it persisted in these tables.

How do I configure Authority.hbm.xml so that the "username" property is of class "UserAccount" but doesn't use the "Id" in the relationship, but "username" instead? That is, how can I map one object to another, and use something other than the Id as the foreign key?

"Authority" is the only object that uses "username" as the foreign key. All the other objects use the "Id" of UserAccount. So I don't want to change UserAccount to use "username" as its primary key.

I am stumped.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 15, 2006 9:28 am 
Beginner
Beginner

Joined: Sat Dec 17, 2005 1:24 pm
Posts: 42
Location: Berlin, Germany
Hi!

You can have foreign key references not only to primary keys but to any UNIQUE, NOT NULL column in another table (this is not a Hibernate restriction, but a general database modelling issue).

Why don't you change your mapping an a way that user_account.id is referenced from authority? One could even think to extract the mapping into a seperate mapping table to allow the same privilege to be shared by several users.

If you want to stay with your mapping, did you have a look at the where="arbitrary sql where condition" property of a mapping (see section 6.2 in the reference manual)?

All the best,

René


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 17, 2006 5:26 am 
Regular
Regular

Joined: Tue Dec 14, 2004 5:21 am
Posts: 104
Location: india
you may use the "property-ref" attribute to point to a field other than primary key
hope this will help u :)

_________________
sHeRiN
thanks for your ratings ...... :)


Top
 Profile  
 
 Post subject: I got it to work
PostPosted: Fri Jan 27, 2006 5:07 pm 
Newbie

Joined: Sun Jan 15, 2006 12:28 am
Posts: 4
In Authority.hbm.xml I have this:

Code:
<many-to-one name="userAccount" column="username" class="UserAccount" not-null="true" property-ref="username" />


and in UserAccount.hbm.xml I have this:
Code:
<set name="authoritySet" inverse="true" cascade="all">
            <key column="username" property-ref="username"/>
            <one-to-many class="Authority"/>
        </set>


In UserAccount.java I added this:
Code:
private java.util.Set authoritySet = new HashSet();
public void addAuthority(Authority authority) {
       getAuthoritySet().add(authority);
    }


and in the code where I use all this, I do this:
Code:
Authority authority = new Authority();
authority.setUserAccount(newUserAccount);
newUserAccount.addAuthority(authority);


Thanks you guys's help.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 5:03 am 
Newbie

Joined: Sun Jan 15, 2006 12:28 am
Posts: 4
....But now I found a new problem. Whenever I try to "saveOrUpdate" the UserAccount, I get an error:

Code:
java.lang.NullPointerException
        at org.hibernate.engine.EntityEntry.getLoadedValue(EntityEntry.java:177)

        at org.hibernate.type.CollectionType.getKeyOfOwner(CollectionType.java:3
04)
        at org.hibernate.engine.Collections.processReachableCollection(Collectio
ns.java:173)
        at org.hibernate.event.def.FlushVisitor.processCollection(FlushVisitor.j
ava:37)


If I remove the mapping in UserAccount.hbm.xml of the Authority set, then the update works just fine for the UserAccount. Yikes. I don't get this at all. Any help would be very much appreciated. I am using hibernate 3.1.2 btw


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 01, 2006 12:50 am 
Newbie

Joined: Sun Jan 15, 2006 12:28 am
Posts: 4
It looks like the second problem I ran into is a bug in hibernate. I changed some hibernate code according to the fixes listed in this thread and it works now:
http://opensource2.atlassian.com/projec ... se/HB-1526


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