-->
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.  [ 3 posts ] 
Author Message
 Post subject: [Resolved] the session.save always return 0
PostPosted: Thu Jun 12, 2008 11:15 am 
Newbie

Joined: Thu Jun 12, 2008 10:52 am
Posts: 16
Hello there !
I read somewhere on the net that session.save should return the primary key of the new row inserted in the DB. But in my situation it always return 0. Here is a sample of my code :

Code:
Session session = HibernateSessionFactory.getSession();
Transaction tr = session.beginTransaction();

Request req = new Request();
req.setAuthor("M. Durant");
System.out.println(session.save(req));
      
Category cat = new Category();
cat.setName("LUXE");
cat.setPrice(30);
System.out.println(session.save(cat));
      
RequestCategory rc = new RequestCategory(req, cat, 5);
session.save(rc);

tr.commit();
session.close();

So when i want to save my RequestCategory, I always get an "duplicate entry" error because it always retrieve an id of 0 with the objects req and cat. Although my req id and cat id are good in the Request and Category tables (because of the mysql auto-increment option)

Could someone explain to me what is happening. And how could i resolve that.
Thanks


Last edited by K-kOo on Thu Jun 12, 2008 1:05 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 12, 2008 1:04 pm 
Newbie

Joined: Thu Jun 12, 2008 10:52 am
Posts: 16
Resolved !
With the right class attribut in the corresponding mapping files.

from:
<class name="Request" table="request" catalog="epirent">
<id name="id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>

to:
<class name="Request" table="request" catalog="epirent">
<id name="id" type="int">
<column name="ID" />
<generator class="identity" />
</id>

Bye


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 12, 2008 4:51 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Yes, the correct GenerationType makes all the difference!


http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=12oneclasstoonetablejpamapping

Image

Quote:
javax.persistence.GenerationType.AUTO

The AUTO generation strategy is the default, and this setting simply chooses the primary key generation strategy that is the default for the database in question, which quite typically is IDENTITY, although it might be TABLE or SEQUENCE depending upon how the database is configured. The AUTO strategy is typically recommended, as it makes your code and your applications most portable.
javax.persistence.GenerationType.IDENTITY

The IDENTITY option simply allows the database to generate a unique primary key for your application. No sequence or table is used to maintain the primary key information, but instead, the database will just pick an appropriate, unique number for Hibernate to assign to the primary key of the entity. With MySQL, the first lowest numbered primary key available in the table in question is chosen, although this behavior may differ from database to database.
javax.persistence.GenerationType.Sequence

Some database vendors support the use of a database sequence object for maintaining primary keys. To use a sequence, you set the GenerationType strategy to SEQUENCE, specify the name of the generator annotation, and then provide the @SequenceGenerator annotation that has attributes for defining both the name of the sequence annotation, and the name of the actual sequence object in the database.
Here's what the getId() method of the Snafu class would look like if we used a SEQUENCE GenerationType:


Code:
@Id
@SequenceGenerator(name="s1", sequenceName="SEQ")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="s1")
public long getId() {return id;}



http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=12oneclasstoonetablejpamapping

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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