-->
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.  [ 7 posts ] 
Author Message
 Post subject: Problem/misunderstanding about ID generator
PostPosted: Wed Sep 07, 2005 2:14 pm 
Newbie

Joined: Wed Sep 07, 2005 1:58 pm
Posts: 6
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.05

Mapping documents:
<hibernate-mapping>
<class name="Event" table="EVENTS">
<id name="id" column="EVENT_ID">
<generator class="native"/>
</id>
<property name="date" type="timestamp" column="EVENT_DATE"/>
<property name="title"/>
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
Event theEvent = new Event();
theEvent.setTitle(title);
theEvent.setDate(theDate);
session.save(theEvent);

Full stack trace of any exception that occurs:

Name and version of the database you are using:
MySQL 4.1

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

I'm a newbie to Hibernate and am following the example at http://www.hibernate.org/hib_docs/v3/reference/en/html/tutorial.html.

I'm confused about the id property of the classes and how it's mapped to a field in the database. I want my table to have an auto_increment primary key, and I want that field mapped to the id field of my Java class. No matter which type of generator I seem to use in the mapping file, my database always auto_increments the PK (1, 2, 3, etc...) but Hibernate always assigns its own values to the id field of my object (4294967297, 4294967298, etc...).

Am I misunderstanding what that id is supposed to be used for? Or am I just not setting up my mapping file correctly?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 07, 2005 2:32 pm 
Beginner
Beginner

Joined: Mon Oct 11, 2004 12:30 pm
Posts: 21
First of all if you want increment, change the generator class to "increment", secondly does your ddl specify "auto increment" on the id column?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 07, 2005 2:40 pm 
Newbie

Joined: Wed Sep 07, 2005 1:58 pm
Posts: 6
Yes I have the id column set to auto_increment. When I use generator as "increment" it assigns the first row 1, then the second row 16777215, then the third insert gets an exception:

14:39:23,834 WARN JDBCExceptionReporter:71 - SQL Error: 1062, SQLState: 23000
14:39:23,844 ERROR JDBCExceptionReporter:72 - Duplicate entry '16777215' for key 1
14:39:23,844 ERROR AbstractFlushingEventListener:277 - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:74)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:181)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at EventManager.createAndStoreEvent(Unknown Source)
at EventManager.main(Unknown Source)
Caused by: java.sql.BatchUpdateException: Duplicate entry '16777215' for key 1
at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:642)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:174)
... 9 more


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 07, 2005 3:55 pm 
Beginner
Beginner

Joined: Mon Oct 11, 2004 12:30 pm
Posts: 21
Have you tried setting an "unsaved-value" default in the constructor of your pojo and on your id element?

Code:
  //constructor
  public Event() {
    id=-1;
  }


Code:
<id name="id" column="EVENT_ID" unsaved-value="-1">
  <generator class="increment"/>
</id>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 07, 2005 4:47 pm 
Newbie

Joined: Wed Sep 07, 2005 1:58 pm
Posts: 6
The "unsaved-value" in the contructor & id element did nothing. I still get an id of 1, then 16777215, then an exception.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 08, 2005 11:03 am 
Newbie

Joined: Wed Sep 07, 2005 1:58 pm
Posts: 6
Is it even possible to have Hibernate assign the id property of your POJO to the actual auto-incremented primary key id field in your database table?!?!

I already have a populated customers table with these fields:
- customer_id (integer, auto_increment)
- customer_name (varchar)

The customer_id field starts at 1 for the first customer and goes up from there to the last customer.

I created a Customer class that has these properties, along with appropriately named getters/setters:
- id (Long)
- name (String)

Now, I want Hibernate to create a Customer object for each row in the customers table, mapping the table fields to these POJO properties:
- customer_id ==> id
- customer_name ==> name

So I put this in the Customer.hbm.xml file:
Code:
<class name="Customer" table="customers">
    <id name="id" column="customer_id">
        <generator class="native"/>
    </id>
    <property name="name" column="customer_name"/>
</class>


However, when Hibernate creates the Customer objects, the id's ARE NOT the ones contained in the customers table. They are integers that start at 4294967297 and count up.

Can anyone tell me what stupid mistake or misunderstanding I'm making? I really want to use Hibernate, it looks super-cool, but I can't seem to get it to do this one really simple little thing.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 08, 2005 11:32 am 
Newbie

Joined: Wed Sep 07, 2005 1:58 pm
Posts: 6
For any other poor newbie like myself who runs into this stupid problem here's the fix:

- declare your id property of your POJO as an int, not a Long as is done in the tutorial referenced above
- use <generator class="increment"/> in the mapping file

Hibernate now correctly:
- uses the existing primary key field to set the value of id in the Customer object
- correctly assigns the id of newly created Customer objects to the next higher value of the primary key when saving the new Customer to the database


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