-->
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.  [ 4 posts ] 
Author Message
 Post subject: Why ignore value of id?
PostPosted: Thu May 10, 2007 4:10 am 
Newbie

Joined: Thu May 10, 2007 1:35 am
Posts: 2
Hi guys,
I'm new to Hibernate, and using Hibernate 3.2.3.

I have a simple domain object -- bo.User, and a Hibernate config file as shown as follows,
Code:
<hibernate-mapping>
    <class name="bo.User" table="user">
        <id name="id" column="id" type="java.lang.Integer">
            <generator class="native"/>
        </id>
        <property name="name" column="name" type="java.lang.String"/>
        <property name="age" column="age" type="java.lang.Integer"/>
    </class>
</hibernate-mapping>

bo.User just has three fields: id, name and age.
I created an User instance by the following codes:
Code:
User user = new User();
user.setId(100);
user.setName("Sha Jiang");
user.setAge(26);

and took advantage of
Code:
Session.save(user);

to insert the data into user table (I'm using MySQL 5.0.27).

All was OK while running the application.
Then I checked the table, and found that the value of field id wasn't 100, but 1.
It seems that Hibernate ignores values of incremented id, is it right?
DBMS allows to set value of incremented id explicitly, why Hibernate doesn't?

a cup of Java, cheers!
Sha Jiang


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 10, 2007 4:47 am 
Beginner
Beginner

Joined: Mon Nov 06, 2006 2:40 am
Posts: 29
Location: New Delhi, India
the reason being, you have explicitly told hibernate to user native for id and at same time you are providing value for id, hibernate is giving preference to native


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 10, 2007 4:57 am 
Regular
Regular

Joined: Mon Mar 26, 2007 12:38 am
Posts: 119
Hi,
>>> It seems that Hibernate ignores values of incremented id, is it right?
Yes And No.
Yes, it ignores for non persistent objects, based on the id generator strategy.
No, it doesn't ignore for persistent objects. You get an exception. ( which means Hibernate doesn't ignore but takes it seriously )

>>> DBMS allows to set value of incremented id explicitly, why Hibernate doesn't?
It is Hibernate philosophy and is related to database identity with Hibernate. Hibernate doesn't allow us to change the identifier of a persistent instance after it is first assigned.

-------------------------------------
Rate the reply if you find it helpful


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 10, 2007 5:37 am 
Newbie

Joined: Thu May 10, 2007 1:35 am
Posts: 2
Thanks!


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