-->
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: Duplicate entry error
PostPosted: Wed Apr 07, 2004 11:10 am 
Newbie

Joined: Mon Apr 05, 2004 6:25 pm
Posts: 5
Hi there:

From my web interface when I insert a word to the database, it works fine. But, when I insert the second it gives me the following error. It seems Hibernate is trying to insert the second one with the same ID as the first one. Does this thing have anything to do with Thread Local? I didn't use that pattern, because it give me error too. I'm new to Hibernate. Please advise.

Below is the error messages and the code that caused them:

error message
-------------------------
2004-04-06 16:29:19,494 net.sf.hibernate.util.JDBCExceptionReporter ERROR-could not insert: [com.blu.words.Word#0238b990fbc1b95400fbc1b9a1720006]java.sql.SQLException: Invalid argument value, message from server: "Duplicate entry '0238b990fbc1b95400fb' for key 1"

2004-04-06 16:29:19,494 net.sf.hibernate.impl.SessionImpl ERROR-Could not synchronize database state with sessionnet.sf.hibernate.JDBCException: could not insert: [com.blu.words.Word#0238b990fbc1b95400fbc1b9a1720006]
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:478)

code
----------------------------------------------------

public void insertWord(WordView view){
Word word = new Word();
word.setWord(view.getWord());
word.setGloss(view.getGloss());
word.setDetail(view.getDetail());
log.debug("insert word: "+word);
try{
Configuration cfg = new Configuration()
.addClass(Word.class);

SessionFactory sf = cfg.buildSessionFactory();

// open session
Session sess = sf.openSession();
Transaction tx = sess.beginTransaction();

sess.save(word);

tx.commit();
sess.close();
}catch(Exception ex){
log.error("in insertWord(): "+ex);
}
}


Top
 Profile  
 
 Post subject: Re: Duplicate entry error
PostPosted: Wed Apr 07, 2004 11:55 am 
Regular
Regular

Joined: Wed Mar 03, 2004 9:38 am
Posts: 70
robert2004 wrote:
It seems Hibernate is trying to insert the second one with the same ID as the first one.


Yes, that would be my guess too.

Quote:
Does this thing have anything to do with Thread Local? I didn't use that pattern,


Syntax error.

How do you generate your id:s? While you're at it, post your mapping documents (the .hbm.xml file(s)) too.

Another thing, if the code you showed is pulled straight from your system you're going to have VERY bad performance. Rebuilding the session factory for every database operation will produce massive overhead.


Top
 Profile  
 
 Post subject: mapping file
PostPosted: Wed Apr 07, 2004 4:05 pm 
Newbie

Joined: Mon Apr 05, 2004 6:25 pm
Posts: 5
Thanks much! Here is the mapping file I used.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="com.blu.words.Word"
table="mywords">

<id name="id" type="string"
unsaved-value="null">
<column name="ID" sql-type="char(20)"
not-null="true"/>
<generator class="uuid.hex"/>
</id>
<property name="word">
<column name="word" sql-type="char(20)"
not-null="true"/>
</property>
<property name="gloss">
<column name="gloss" sql-type="char(50)"
not-null="true"/>
</property>
<property name="detail">
<column name="detail" sql-type="char(255)"
not-null="false"/>
</property>
<property name="created">
<column name="created" sql-type="date"
not-null="true"/>
</property>
<property name="reviewed">
<column name="reviewed" sql-type="integer"
not-null="true"/>
</property>
<property name="degree">
<column name="degree" sql-type="integer"
not-null="true"/>
</property>
</class>
</hibernate-mapping>


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.