-->
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.  [ 6 posts ] 
Author Message
 Post subject: MySQL and Identity generator problem
PostPosted: Thu Feb 05, 2004 5:32 am 
Beginner
Beginner

Joined: Thu Feb 05, 2004 4:59 am
Posts: 23
I use the following construction:
MyObject obj = new MyObject();
obj.setSomething("something");

Then:
Session sess = HibernateUtil.currentSession();
sess.save(test);
sess.flush();

but the above code works only once, then table is empty. my object receives id=0;
then this code is invoked in second time - i receive exception:

12:25:22,968 WARN JDBCExceptionReporter:38 - SQL Error: 1062, SQLState: S1009
12:25:22,968 ERROR JDBCExceptionReporter:46 - Invalid argument value, message from server: "Duplicate entry '0' for key 1"
12:25:22,984 WARN JDBCExceptionReporter:38 - SQL Error: 1062, SQLState: S1009
12:25:22,984 ERROR JDBCExceptionReporter:46 - Invalid argument value, message f
rom server: "Duplicate entry '0' for key 1"
12:25:23,000 ERROR JDBCExceptionReporter:38 - could not insert: [com.overalltest
ing.hibernate.objects.Test]
java.sql.SQLException: Invalid argument value, message from server: "Duplicate
entry '0' for key 1"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1697)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1083)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1142)
at com.mysql.jdbc.Connection.execSQL(Connection.java:1876)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.ja
va:1590)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java
:1653)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java
:1492)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.jav
a:502)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.jav
a:433)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:876)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:817)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(Session
Impl.java:737)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:717)
at com.overalltesting.servlets.TestDetailsParser.doPost(Unknown Source)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:165)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:103)
at com.caucho.server.http.FilterChainServlet.doFilter(FilterChainServlet
.java:96)
at com.caucho.server.http.Invocation.service(Invocation.java:315)
at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:246
)
at com.caucho.server.http.HttpRequest.handleConnection(HttpRequest.java:
164)
at com.caucho.server.TcpConnection.run(TcpConnection.java:139)
at java.lang.Thread.run(Thread.java:534)

WHAT IS THE PROBLEM ?

in hbm.xml i have:
<class name="com.overalltesting.hibernate.objects.MyObject" table="MY_TABLE">
<id name="id" type="int" column="ID" unsaved-value="0">
<generator class="identity"/>
</id>
<property name="something" type="string">
<column name="SOMETHING" sql-type="TEXT" not-null="true"/>
</property>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2004 6:29 am 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
Are you sure your field is of type AUTO_INCREMENT in your database ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2004 6:31 am 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
Hint:
try to remove the unsaved-value directive in your mapping config... God feeling ;-)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2004 6:39 am 
Beginner
Beginner

Joined: Thu Feb 05, 2004 4:59 am
Posts: 23
brenuart wrote:
Are you sure your field is of type AUTO_INCREMENT in your database ?


Damn, is it should be AUTO_INCREMENT ? I thought incremention is Hibernate's job :(


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2004 6:43 am 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
whizz_kid wrote:
brenuart wrote:
Are you sure your field is of type AUTO_INCREMENT in your database ?


Damn, is it should be AUTO_INCREMENT ? I thought incremention is Hibernate's job :(


Nope - the INCREMENT generator uses the database facilities...
If you want Hibernate to generate the ids itself, use another generator like HiLo, Increment, etc.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2004 6:56 am 
Beginner
Beginner

Joined: Thu Feb 05, 2004 4:59 am
Posts: 23
Quote:
Nope - the INCREMENT generator uses the database facilities...
If you want Hibernate to generate the ids itself, use another generator like HiLo, Increment, etc.


Done, it's working now... thanks a lot !


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