-->
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: Set parents id on child on insert
PostPosted: Wed May 12, 2004 3:37 pm 
Newbie

Joined: Tue Apr 06, 2004 6:37 am
Posts: 17
Location: Oslo, Norway
I have two objects that have a one-to-one relation. When I'm adding them to the DB I first create a separate instance of the two objects. The I assign the child object to the parent. The problem is that when inserted into the db (MySQL) the child does not get the parents id.

Simplified example of my code:
Code:
User user = new User(username,password);
Person person = new Person(name, address);
u.setPerson(person);

session.save(user);
session.flush();


I then get the exception General error, message from server: "Column 'userID' cannot be null". The userID field has the not null constraint, so I understand the message.

My question is: how can I assign the right userID to the person. My workaround is to set some static userID on the person object and then after the user is saved, retrieve the right userID and update the person.

There must be a better way to do this?


Mapping file for user (parent)
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping >
    <class name="package.User" table="user" >
    <id name="userID" column="userID">
        <generator class="foreign">
            <param name="property">person</param>
        </generator>
    </id>

       <property name="username" column="username" type="java.lang.String" />
       <property name="password" column="password" type="java.lang.String" />

      <one-to-one name="person" class="package.Person" property-ref="brukerID" />
    </class>
</hibernate-mapping>


Mapping file for Person (child)
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping >
    <class name="package.Person" table="person" >
      <id name="personID" column="personID" type="int" unsaved-value="0">
         <generator class="identity"/>
      </id>
       <property name="userID" column="userID" type="int" />
       <property name="name" column="name" type="java.lang.String" />
       <property name="address" column="adress" type="java.lang.String" />
    </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 12, 2004 6:19 pm 
Regular
Regular

Joined: Thu Jan 29, 2004 10:34 am
Posts: 52
Location: Austin, TX
the data model seems incorrect.

remove the line

Code:
<property name="userID" column="userID" type="int" />


from your Person mapping. a one-to-one is simply a primary key association.

considering the general nature of a Person object it's usually not owned by other objects. in this case consider a many-to-one mapping where the person key is on the source object of the association.

http://www.xylax.net/hibernate/

also you are better off using -1 as the unsaved-value instead of 0 (remeber to intialize the value of id in your POJO)


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 13, 2004 7:37 am 
Newbie

Joined: Tue Apr 06, 2004 6:37 am
Posts: 17
Location: Oslo, Norway
Thank you! Works like a charm!


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 13, 2004 2:54 pm 
Newbie

Joined: Tue Apr 06, 2004 6:37 am
Posts: 17
Location: Oslo, Norway
But... I tried to fix some more of my one-to-one assosiations aswell..

In my user object I also have an object which holds the users access. The problem is that when I update the user object, the useraccess object is not updated.

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping >
    <class name="auth.User" table="user" >
   <id name="userID" column="userID">
        <generator class="foreign">
            <param name="property">person</param>
        </generator>
    </id>

       <property name="username" column="username" type="java.lang.String" />
       <property name="password" column="password" type="java.lang.String" />

      <one-to-one name="person" class="auth.Person"/>
       <one-to-one name="useraccess" class="auth.UserAccess"/>

      <set name="groups" table="userGroup">
            <key column="userID" />
            <many-to-many column="groupID" class="auth.Group" />
        </set>

    </class>
</hibernate-mapping>


The useraccess object is mapped like this:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping >
    <class name="auth.UserAccess" table="useraccess" >
      <id name="userID" column="brukerID" type="int" unsaved-value="0">
         <generator class="identity"/>
      </id>

       <property name="stuff" column="stuff" type="java.lang.String" />

    </class>
</hibernate-mapping>


When I do an update or save on the person object, nothing happens to the UserAccess object.

When I assign a person to the user object, the user is saved just fine.

Can anyone see what is missing?


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.