-->
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.  [ 1 post ] 
Author Message
 Post subject: Generated composite key problem
PostPosted: Fri Dec 26, 2008 9:56 pm 
Newbie

Joined: Fri Dec 26, 2008 9:45 pm
Posts: 1
I'm new to Hibernate, so excuse the novice-ness.

I'm working with table that has a composite-id that is generated by the database. When I create a new object and persist it, the id in the object is not changed. How can I get the value generated for the two columns?

Mapping documents:
Drp1Node.hbm.xml
Code:
<hibernate-mapping>
    <class name="model.Drp1Node" table="drp1_node" catalog="db9551_spin_drupal">
        <composite-id name="id" class="model.Drp1NodeId">
            <key-property name="nid" type="int">
                <column name="nid" />
            </key-property>
            <key-property name="vid" type="int">
                <column name="vid" />
            </key-property>
        </composite-id>
        <property name="type" type="string">
            <column name="type" length="32" not-null="true" />
        </property>
        <property name="title" type="string">
            <column name="title" length="128" not-null="true" />
        </property>
        <property name="uid" type="int">
            <column name="uid" not-null="true" />
        </property>
        <property name="status" type="int">
            <column name="status" not-null="true" />
        </property>
        <property name="created" type="int">
            <column name="created" not-null="true" />
        </property>
        <property name="changed" type="int">
            <column name="changed" not-null="true" />
        </property>
        <property name="comment" type="int">
            <column name="comment" not-null="true" />
        </property>
        <property name="promote" type="int">
            <column name="promote" not-null="true" />
        </property>
        <property name="moderate" type="int">
            <column name="moderate" not-null="true" />
        </property>
        <property name="sticky" type="int">
            <column name="sticky" not-null="true" />
        </property>
    </class>
</hibernate-mapping>



Code between sessionFactory.openSession() and session.close():
Code:
      Drp1NodeId newId = new Drp1NodeId();
      System.out.println("id1: "+newId.getNid());
      Drp1Node newNode = new Drp1Node();
      newNode.setId(newId);
      System.out.println("id2: "+newId.getNid() + "("+newNode.getId().getNid()+")");

      Drp1NodeId savedId = (Drp1NodeId)HibernateUtil.save(newNode);
      System.out.println("id3: "+newId.getNid() + "("+newNode.getId().getNid()+")");
      
      HibernateUtil.getSessionFactory().getCurrentSession().flush();
      System.out.println("id5: "+newId.getNid() + "("+newNode.getId().getNid()+")");


System.out

id1: 0
id2: 0(0)
id3: 0(0)
Hibernate: insert into db9551_spin_drupal.drp1_node (type, title, uid, status, created, changed, comment, promote, moderate, sticky, nid, vid) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
id5: 0(0)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.