-->
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: one-to-one: objects get updated but should be inserted
PostPosted: Sat Dec 25, 2004 6:30 pm 
Newbie

Joined: Sat Dec 25, 2004 6:03 pm
Posts: 2
Hi!

I'm trying to build a simple one-to-one relation in my application. After
some hours of trial and error it nearly works, but a last problem is still
unsolved.
I reduced the whole thing to a simple example:

Class User has a one-to-one relation to class Stats:

Code:
public class User {

//    CREATE TABLE users ( id   BIGINT   NOT NULL,                   
//                         name CHAR(30),
//    PRIMARY KEY(id))

    private long id;
    private String name;
    private Stats stats;
   
    public User(){ }
    [...] // getters and setters for all attributes
}

public class Stats {

//    CREATE TABLE stats (  userid  BIGINT NOT NULL,                               
//                                      value   INT ,
//            PRIMARY KEY(userid),
//            FOREIGN KEY(userid)  REFERENCES users(id) )

    private long id;
    private int  value;
    private User user;
   
    public Stats(){ }
    [...] // getters and setters for all attributes
}


So there is a bidirectional relation between these two classes.
User uses a TableHiLoGenerator for creating the primary key (id).
The PK of Stats (which is also a foreign key to Users) shall be the same
as the associated User object.
Here's the mapping-setup:

Code:
<hibernate-mapping package="persistence">

  <class name="User" table="users" proxy="User" dynamic-update="true">
      
      <id name="id" type="long" column="id" unsaved-value="0">
         <generator class="org.hibernate.id.TableHiLoGenerator">
               <param name="table">uid_table</param>
               <param name="column">next_hi_value_column</param>
         </generator>
      </id>

      <property name="name"      column="`name`" />

      <one-to-one name="Stats"
                class="persistence.Stats"                
               cascade="all"               
               foreign-key="userid"
               constrained="true"   />
   </class>
   
</hibernate-mapping>

-----------------------------------------

<hibernate-mapping package="persistence">

   <class name="Stats" table="stats" proxy="Stats" dynamic-update="true">
      
      <id name="id" column="userid">
         <generator class="foreign">
            <param name="property">user</param>
         </generator>
      </id>

      <one-to-one name="user" class="User" constrained="true" cascade="all"/>

      <property name="value" column="`value`"/>

   </class>
   
</hibernate-mapping>



Next is a short program to test the design:
Code:
    public static void main(String[] args) {
        SessionFactory sessions = new Configuration().configure().buildSessionFactory();
       
        User u = new User();
        u.setName("test");
        Stats s = new Stats();
        u.setStats(s);
        s.setUser(u);
       
        Session ses = sessions.openSession();       
        Transaction tx = ses.beginTransaction();

        Object id = ses.save(u);
        tx.commit();       
        ses.close();
    }


And finally the output of the test-program:

Code:
25.12.2004 23:11:03 org.hibernate.impl.SessionFactoryImpl checkNamedQueries
INFO: Checking 0 named queries
Hibernate: insert into users (`name`, id) values (?, ?)
Hibernate: update stats set `value`=? where userid=?
25.12.2004 23:11:03 org.hibernate.jdbc.BatchingBatcher doExecuteBatch
FATAL: Exception executing batch:
org.hibernate.HibernateException: Batch update row count wrong: 0
   at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:64)
...


First hibernate inserts the newly created User into the database. That's good. But then it tries to update a stats-entry which doesn't exist yet.
There should be a "insert into stats..", not an update.

Where's the mistake? What should I do?
I'm using hibernate 3.0 beta 1.

Thank you for your help.

zinfandel


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 25, 2004 6:36 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Learn about how to use unsaved-value="xxx"


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 25, 2004 7:32 pm 
Newbie

Joined: Sat Dec 25, 2004 6:03 pm
Posts: 2
michael wrote:
Learn about how to use unsaved-value="xxx"


Thank you, michael.
I added 'unsaved-value="0"' to the id-tag of Stats.
After doing so I got a different exception (even before hibernate tries to send the SQL-commands):

Code:
INFO: Checking 0 named queries
Exception in thread "main" org.hibernate.PropertyValueException: not-null property references a null or transient value: persistence.Stats.user
   at org.hibernate.engine.Nullability.checkNullability(Nullability.java:70)
   at org.hibernate.event.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:217)
   at org.hibernate.event.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:148)
   at org.hibernate.event.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:97)
...


So Stats.user is null or transient. I suppose it is treated as transient since having 0 as id-value.
I fixed this one by setting the "contrained" attribute of User's one-to-one tag to "false".
Now it works fine (so far) -- and I hope this is a proper design of my data model in hibernate.

Thanks again,

zinfandel


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.