-->
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.  [ 2 posts ] 
Author Message
 Post subject: Using primitive int or java.lang.Integer for key field?
PostPosted: Fri Aug 11, 2006 5:29 pm 
Beginner
Beginner

Joined: Fri Jul 28, 2006 12:01 pm
Posts: 21
I have an Oracle database table

Account
-------------
Acct_ID - Integer
Acct_Num - varchar2
Acct_Name - varchar2

I use an Oracle sequence to generate Acct_ID

I have the mapping document set up this way

Code:
<!DOCTYPE hibernate-mapping PUBLIC '-//Hibernate/Hibernate Mapping DTD 3.0//EN'
'http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd'>
<hibernate-mapping>
    <class name="com.stlouiscity.budget.database.beans.AccountBean" table="Budget.Account">
        <id name="acctId" column="Acct_ID" type="integer" >
            <generator class="sequence">
                <param name="sequence">Acct_ID_SQ</param>
            </generator>
        </id>
        <property name="acctNum" type="string" column="Acct_Num"></property>
        <property name="acctName" type="string" column="Acct_Name"></property>
    </class>
       
</hibernate-mapping>


I wish to use the saveOrUpdate convienence method to insert/update records. What I want to know is how do I initialize my bean's id field? If I use int then I have to initialize it to 0. How will saveOrUpdate know how to issue an insert and use the sequence if the value is already popluated as zero? Won't Hibernate just issue an update? Should I use a java.lang.Integer instead so it can be initialized to null? If yes, how do I map this in the type attribute in the hbm file for the id field? Any guidance on how to handle inserts


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 11, 2006 6:17 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
I always use the primitive wrappers for my identifier columns. That way, your unsaved value is null. Your bean should leave that field as null when you instantiate it and Hibernate will get the correct value from the sequence on insertion. The saveOrUpdate checks the identifier field before determining whether to do a insert or update based on the unsaved-value property of the identifier (which is null by default for primitive wrappers). If you really must use a primitive, add the unsaved-value="0" in the id definition, e.g.

Code:
<id name="acctId" column="Acct_ID" type="integer" unsaved-value="0">
...


Most of the time, however, you do not need to specify the type or the unsaved-value, as Hibernate will reflect the mapped class and discover it's types in that manner.


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