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: