-->
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.  [ 5 posts ] 
Author Message
 Post subject: Generated value for NON-PRIMARY column
PostPosted: Mon Jul 18, 2005 11:27 am 
Newbie

Joined: Mon Jul 18, 2005 11:09 am
Posts: 3
I have a table that has a primary key and another value that is not a primary/foreign key, but is set as a not-null IDENTITY that is incremented by 1.

How do I set this up in my mappings? This is what I am currently doing (originally generated from the database schema using a tool):

Code:
<hibernate-mapping>
    <class name="com.mycompay.dto.History" table="history">
        <id name="historystepId" type="long">
            <column name="historystep_id" />
            <generator class="native" />
        </id>
        <property name="historyOrder" type="long">
            <column name="history_order" not-null="true" unique="true" />
        </property>
    </class>
</hibernate-mapping>


In the above historystepId is the primary key, and historyOrder is just another column in the database that happens to be an IDENTITY. But this mapping does not work because historyOrder is not-null and passing null to a not-null property causes hibernate to throw an error when inserting.

Normally with a auto-incremented primary key you can set the id to null and have the database generate the id for you and hibernate gladly passes the generated id back to you. Is there any way to get this non-primary identity column to work in hibernate3?

(I already tried removing not-null in the historyOrder property mapping)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 18, 2005 11:42 am 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
Have you tried adding insert="false" update="false" to the property? that should keep Hibernate from throwing the error, but I don't think historyOrder will get updated with the generated value on insert. You would have to do a Session.refresh() to see the value, probably.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 18, 2005 11:52 am 
Newbie

Joined: Mon Jul 18, 2005 11:09 am
Posts: 3
Thanks, I gave that a shot but hibernate still complains:


org.hibernate.PropertyValueException: not-null property references a null or transient value: com.mycompany.dto.history.historyOrder


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 18, 2005 12:01 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
Oh, you might have to change the type from long to java.lang.Long. I've gotten it to work with a java.lang.String. I didn't have unique="true", though, so you could see if removing that helps, too.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 18, 2005 3:26 pm 
Newbie

Joined: Mon Jul 18, 2005 11:09 am
Posts: 3
You were right, I was putting update=false and insert=false in the wrong place, but this worked:

Code:
<property name="historyOrder" type="java.lang.Long" insert="false" update="false">
            <column name="history_order" not-null="true" unique="true" />
        </property>


Thanks for your help Nathan!


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