-->
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.  [ 7 posts ] 
Author Message
 Post subject: one-to-one relationship help
PostPosted: Mon Mar 02, 2009 12:29 pm 
Newbie

Joined: Wed Feb 25, 2009 5:09 pm
Posts: 8
Hi,

I'm trying to do a one-to-one mapping relationship between a User and a UserProfile. One user can only have one profile and one profile can only belong to one user. I get the following error when trying to add a new user:

attempted to assign id from null one-to-one property: Users System.Exception {NHibernate.Id.IdentifierGenerationException}

My mapping files are below:


Users.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">

<class name="Users, App_Code/Mappings" lazy="true" table="users">
<id name="UserID" column="user_id" type="Int32">
<generator class="identity">
</generator>
</id>
<property name="FirstName" column="first_name" type="String" length="50"/>
<property name="LastName" column ="last_name" type="String" length="50"/>
<property name="UserName" column ="user_name" type="String" length="50"/>
<property name="EmailAddress" column ="email_address" type="String" length="50"/>
<property name="DOB" column="dob" type="DateTime"/>
<property name="Gender" column="gender" type="boolean"/>
<property name="CreationDate" column="creation_date" type="DateTime"/>
<property name="LastLogin" column="last_login_date" type="DateTime"/>
<property name="Password" column="user_password" type="String" length="100"/>


<many-to-one name="Country" column="country_id" class="Countries, App_Code/Mappings" cascade ="none"/>
<one-to-one name="UsersProfile" class="UsersProfile, App_Code/Mappings" cascade="all"/>

</class>


</hibernate-mapping>

UsersProfile.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">

<class name="UsersProfile, App_Code/Mappings" lazy="true" table="Users_Profile">
<id name="UserID" type="Int32" column="user_id">
<generator class="foreign">
<param name="property">Users</param>
</generator>
</id>
<property name="Website" column="website_url" type="String" length="100"/>
<property name="Occupation" column ="occupation" type="String" length="100"/>
<property name="Hobbies" column="hobbies" type="String" length ="100"/>

<one-to-one name="Users" class="Users, App_Code/Mappings" constrained ="true"/>

</class>


</hibernate-mapping>

My User.cs returns a UserProfile object:

public virtual UsersProfile UsersProfile
{
get { return userProfile; }
set { userProfile = value; }
}

My UsersProfile.cs returns a Users object:


public virtual Users Users
{
get { return _user; }
set { _user = value; }
}

The AddNewUser code is below:

public bool AddNewUser(Users lUser)
{
ISession session = NHibernateHelper.GetCurrentSession();
ITransaction tx = null;
bool success = false;
lUser.UsersProfile = new UsersProfile();
lUser.UsersProfile.UserID = lUser.UserID;
try
{
tx = session.BeginTransaction();
session.Save(lUser);
tx.Commit();
success = true;
}
catch (Exception ex)
{
tx.Rollback();
success = false;
}

return success;

}

So basically what i am attempting to do is when I add a user, i want a user profile to be added with the same id. My user table has a primary key user_id and my userprofile table has a primary and foreign key of user_id.

Hopefully you can help me out...i have spent alot of time trying to figure this one out.

Cheers


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 03, 2009 11:42 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Try

lUser.UsersProfile.Users = lUser;

instead of

lUser.UsersProfile.UserID = lUser.UserID;

You specified the "Users" property for id generator "foreign". Hibernate then will use this property to get the id and in your case it's null.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 03, 2009 2:18 pm 
Newbie

Joined: Wed Feb 25, 2009 5:09 pm
Posts: 8
thanks..it worked....cheers


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 01, 2009 4:45 am 
Newbie

Joined: Thu Nov 06, 2008 10:42 am
Posts: 5
Thx wolli, I was too stuck on the same issue and it worked for me.

But I had to make my one-to-one association bi-directional. Any idea, if I would like to achieve the same thing in unidirectional mapping ie. UserProfile doesnt have User as an attribute.

I assume that foreign key generation requires a property ( in this case "Users") which wouldnt be available in case of uni-directional.

Please help me out..


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 01, 2009 4:48 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Hmm, not sure. Post your mappings.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 01, 2009 6:32 am 
Newbie

Joined: Thu Nov 06, 2008 10:42 am
Posts: 5
This is relevant mapping for Party Entity.

Code:
@Entity
@Table(name = "PARTY")
public class Party{
   private Person person;
   private String key;
   @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL,)
    @PrimaryKeyJoinColumn(name = "PARTY_ID", referencedColumnName = "PERSON_PARTY_ID")
    public Person getPerson() {
        return person;
    }
   
    public void setPerson(Person person) {
        this.person = person;
    }

    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    @Column(name = "PARTY_ID", nullable = false)
    public String getKey() {
        return key;
    }
   
    private void setKey(String key) {
        this.key = key;
    }
}


and it is one-to-one mapped to Person as:

Code:
@Entity(name = "Person")
@Table(name = "PERSON")
public class Person {

   private String key;
     @Id
     @Column(name = "PERSON_PARTY_ID")
    public String getKey() {
        return key;
    }
   
    public void setKey(String key) {
        this.key = key;
    }
   
}


Now in Person, I deliberately dont have sysUUID as generated as then it generates a unique value for person and the association gets lost. With this mapping hibernate throws the following exception :

Caused by: org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): Person.

-thanks again


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 01, 2009 10:52 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You probably have to specify the generator "foreign" like j2flk has in his example:

<id name="UserID" type="Int32" column="user_id">
<generator class="foreign">
<param name="property">Users</param>
</generator>
</id>

But, I don't know how to do that with attributes.

_________________
--Wolfgang


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