-->
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.  [ 10 posts ] 
Author Message
 Post subject: Unable to get quickstart example to work
PostPosted: Sat Jul 09, 2005 10:16 pm 
Newbie

Joined: Sat Jul 09, 2005 10:07 pm
Posts: 6
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

I can't get the quickstart example from the manual to work correctly. I am running on Tomcat 5.5.9 and used the following SQL script to create my table in MySQL. I created the column as auto_increment and used the generator class="increment" in my mapping file but continue to get the error:

identifier of an instance of org.hibernate.examples.quickstart.Cat altered from 2 to null

I am not setting the identifier - I am assuming it will be generated by the database since the column is autoincrement. Also, if I manually insert a row into the table and then try and do a select on the table, using the code provided by the manual, I also get the same error. Why is it thinking that I am setting an Id and what can I do to fix this problem?

Thanks for the help - this has been rather frustrating

=======

create table cat (
cat_id bigint not null auto_increment,
name varchar(16) not null,
sex char(1) not null,
weight float(2),
primary key (cat_id)
);

Hibernate version: 3.0.5

Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.hibernate.examples.quickstart.Cat" table="CAT">

<id name="id" column="CAT_ID" type="java.lang.Long">
<generator class="increment"/>
</id>
<!-- A cat has to have a name, but it shouldn' be too long. -->
<property name="name">
<column name="NAME" length="16" not-null="true"/>
</property>
<property name="sex"/>
<property name="weight"/>
</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
Cat princess = new Cat();
princess.setName("Sylvester");
princess.setSex('F');
princess.setWeight(7.4f);
session.save(princess);
tx.commit();
HibernateUtil.closeSession();


Full stack trace of any exception that occurs:
org.hibernate.HibernateException: identifier of an instance of org.hibernate.examples.quickstart.Cat altered from 2 to null
at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:51)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:82)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:190)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:70)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
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 org.hibernate.examples.quickstart.QuickstartServlet.doGet(QuickstartServlet.java:61)


Name and version of the database you are using:
MySQL 4.1.12a


The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 09, 2005 10:41 pm 
Beginner
Beginner

Joined: Fri Jul 08, 2005 12:38 pm
Posts: 41
Location: Massachusetts, USA
The problem is that MySQL and Hibernate are competing with each other over who gets to assign the ID.

The auto_increment attribute in MySQL tells the database to assign a unique ID to new rows.

The generator class "increment" in your class mapping tells Hibernate to assign a unique ID using the increment algorithm.

Your code is not setting an ID per se, but both Hibernate and MySQL are doing so on your behalf, and Hibernate doesn't like it because it can't verify that the data was synchronized with 100% accuracy.

To resolve this, you have a few options:

- use generator class="identity" in your mapping
- use generator class="native" in your mapping. I think for MySQL it will use identity
- remove the "auto_increment" property from your ID column in MySQL, and continue to use the increment algorithm

Hope this helps!

Jeremy


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 10, 2005 12:04 am 
Newbie

Joined: Sat Jul 09, 2005 10:07 pm
Posts: 6
That makes a lot of sense, so I ran some scenarios based on what you say. I am still unable to insert a row, but at least in a couple of cases got a different error message. So if I take the auto-increment off the column for the table, it doesn't seem to get the id generated by Hibernate. Is there something else that I am supposed to do?

Thanks
Briann

database column auto-increment and generator identity - [b]org.hibernate.HibernateException: identifier of an instance of org.hibernate.examples.quickstart.Cat altered from 1 to null
org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:51)
org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:82)
org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:190)
org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:70)
org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)

[b]database column no auto increment and generator identity - [b]org.hibernate.HibernateException: The database returned no natively generated identity value
org.hibernate.id.IdentifierGeneratorFactory.getGeneratedIdentity(IdentifierGeneratorFactory.java:33)
org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1761)
org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2178)
org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:34)
org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)

[b]database column auto-increment and generator native - [b]org.hibernate.HibernateException: identifier of an instance of org.hibernate.examples.quickstart.Cat altered from 1 to null
org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:51)
org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:82)
org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:190)
org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:70)
org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)

[b]database column no auto increment and generator native - [b]org.hibernate.HibernateException: The database returned no natively generated identity value
org.hibernate.id.IdentifierGeneratorFactory.getGeneratedIdentity(IdentifierGeneratorFactory.java:33)
org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1761)
org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2178)
org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:34)
org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 10, 2005 12:26 am 
Beginner
Beginner

Joined: Fri Jul 08, 2005 12:38 pm
Posts: 41
Location: Massachusetts, USA
Did you remove the auto_increment attribute AND change the generator class to "native"? You should only do one or the other, not both.

Try it without auto_increment, and with generator class="increment". That should work.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 10, 2005 8:18 am 
Newbie

Joined: Sat Jul 09, 2005 10:07 pm
Posts: 6
Yeah, I tried it both ways-
I removed the auto-increment and changed the generator to native.
I removed the auto-increment and changed the generator to increment

then I tried it with auto-increment and the two different generator types.

Does it make any difference what type of MySQL database is installed - MyISAM or InnoDB? Sorry I may be grasping at straws. I assume there is nothing else in the database to do? The attribute in my Cat class is defined as a Long, does that make a difference?

Thanks for the help
Brian


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 10, 2005 12:28 pm 
Beginner
Beginner

Joined: Fri Jul 08, 2005 12:38 pm
Posts: 41
Location: Massachusetts, USA
I don't if it matters what type of MySQL database you use. My Hibernate experience is limited to Postgres and SQL Server so far.

Are you 100% sure that you aren't calling setId anywhere? Try putting a breakpoint in that method just to be sure.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 10, 2005 5:24 pm 
Newbie

Joined: Sat Jul 09, 2005 10:07 pm
Posts: 6
There is no set anywhere. Of course when the Cat object is created, java initializes the Long id field to null. That shouldn't count as a set though (right?). I'm just doing:

Cat princess = new Cat();
cat.setName(...);
cat.setSex(...);
cat.setWeight(...);

The actual id attribute is :

private Long id;

thanks for the help
Brian


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 10, 2005 5:48 pm 
Beginner
Beginner

Joined: Fri Jul 08, 2005 12:38 pm
Posts: 41
Location: Massachusetts, USA
When you have auto_increment off and use generator class="increment", what error do you get?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 11, 2005 7:09 am 
Newbie

Joined: Sat Jul 09, 2005 10:07 pm
Posts: 6
I get the same error message:

org.hibernate.HibernateException: identifier of an instance of org.hibernate.examples.quickstart.Cat altered from 1 to null
org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:51)
org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:82)
org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:190)
org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:70)
org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)

Thanks for the help
Brian


Top
 Profile  
 
 Post subject: I'm having the same problem with Hibernate 3 and MSSQL
PostPosted: Tue Jan 31, 2006 10:43 am 
Newbie

Joined: Mon Jan 30, 2006 5:23 pm
Posts: 2
Where you able to find a solution to your problem?
I'm new to hibernate and I'm having the same problem as you were having.
I'm able to update, delete and retrieve records from my database but when I try to insert a record I get the error: The database returned no natively generated identity value


Thanks,


N Rodriguez


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