-->
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: UserType as key-property -> value not updated on save
PostPosted: Tue Jan 25, 2005 1:24 pm 
Newbie

Joined: Tue Dec 14, 2004 8:30 am
Posts: 9
Hi there ,

I use the following mapping for a composite id

Mapping documents:

Code:
<composite-id unsaved-value="any">
   <key-property name="begDate"  column="BEG_DATE"  type="java.util.Date"    length="7"/>
   <key-property name="id"       column="ID"        type="java.lang.Integer" length="8"/>
   <key-property name="recordId" column="RECORD_ID" type="com.xxx.usertypes.PrimaryKeyIdType"/>
</composite-id>


As you can see I use a custom UserType for one of the key properties

type="com.xxx.usertypes.PrimaryKeyIdType"

This usertype calls an oracle sequence to generate a new value for that property and this works as expected and this is the code for the nullSafeSet()

Code:
    public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
        String sql = "SELECT seq_the_seq.nextval from dual ";
        Statement s = st.getConnection().createStatement();
        ResultSet rs = s.executeQuery(sql);

        if (value == null) {
            if (rs.next()) {
                int nextVal = rs.getInt(1);
                value = new Integer(nextVal);
                st.setInt(index, nextVal);
                rs.close();
            } else {
                log.error("ResultSet was empty, after trying to generate next ID value ");
            }
        } else {
            st.setInt(index, ((Integer) value).intValue());
        }
    }



So Session.save(Object o) creates a new row as expected.

My problem is now, that after saving the object, it's value for that one property recordId is still null.

What do I need to do to ensure that the key-property will be set with the value created by the sequence ?

Is the UserType the right approach for that our should I go with CompositeUserType ?

I know that the DB-design may look strange but that's the way I have to live with.

Thanks in advance,
Dirk


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.