-->
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: strange ConstraintViolationException
PostPosted: Tue Jun 14, 2005 2:31 pm 
Beginner
Beginner

Joined: Fri Feb 04, 2005 2:44 pm
Posts: 25
Location: Buffalo, NY
Hello

I get this error that doesn't make any sense to me at all. The table that I am inserting into is keyed on study and daset. The records I am inserting have different daset's. Any ideas? I am using Eclipse's Hibernate Synchronizer plugin.

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

<hibernate-mapping package="org.firstbank.common.legacyConverter.tables">
<class name="T_ddwindow" table="ddwindow">

<composite-id name="t_ddwindowID" class="T_ddwindowID">
<key-property name="study"
column="study"
type="string"/>
<key-property name="daset"
column="daset"
type="string"/>
</composite-id>

<property name="nweek" type="int">
<column name="nweek" sql-type="integer" not-null="true"/>
</property>

</class>

<query name="ddwindowCount">
<![CDATA[
select count(*)
from T_ddwindow as ddwindow
where ddwindow.t_ddwindowID.study = :study
]]>
</query>

</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
try
{
session = SessionManager.getSessionFactory().openSession();
tx = session.beginTransaction();
ddwindowList = getDdwindowList();
T_ddwindowDAO ddwindowDAO = new T_ddwindowDAO();
for (int i=0; i< ddwindowList.size(); i++)
{
t_ddwindow = (T_ddwindow) ddwindowList.get(i);
ddwindowDAO.save(t_ddwindow, session);
}

tx.commit();
}
catch (HibernateException he)
{
logger.fatal(he.getMessage(), he);
logger.info("t_ddwindow = [" + t_ddwindow.getT_ddwindowID().getStudy() + ", " +
t_ddwindow.getT_ddwindowID().getDaset() + ", " +
t_ddwindow.getNweek() + "]");
logger.info(Constants.programName + " Ended");
SessionManager.disconnect();
System.exit(1);
}
finally
{
try
{
session.close();
}
catch (HibernateException he)
{
logger.error(he.getMessage());
}
}
Full stack trace of any exception that occurs:
2005-06-14 14:01:23,140 [main] FATAL - could not insert: [org.firstbank.common.legacyConverter.tables.T_ddwindow#org.firstbank.common.legacyConverter.tables.T_ddwindowID@7e46eafb]
net.sf.hibernate.exception.ConstraintViolationException: could not insert: [org.firstbank.common.legacyConverter.tables.T_ddwindow#org.firstbank.common.legacyConverter.tables.T_ddwindowID@7e46eafb]
at net.sf.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:63)
at net.sf.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:30)
at net.sf.hibernate.persister.AbstractEntityPersister.convert(AbstractEntityPersister.java:1332)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:474)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:438)
at net.sf.hibernate.impl.ScheduledInsertion.execute(ScheduledInsertion.java:37)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2438)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2391)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2260)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at org.firstbank.common.legacyConverter.convertDDWINDOW.ConvertDDWINDOW.load(ConvertDDWINDOW.java:365)
at org.firstbank.common.legacyConverter.convertDDWINDOW.ConvertDDWINDOW.main(ConvertDDWINDOW.java:95)
Caused by: ca.edbc.util.EdbcEx: Duplicate key on INSERT detected.
at ca.edbc.jdbc.EdbcObj.readResults(EdbcObj.java:782)
at ca.edbc.jdbc.EdbcObj.readResults(EdbcObj.java:573)
at ca.edbc.jdbc.EdbcPrep.exec(EdbcPrep.java:771)
at ca.edbc.jdbc.EdbcPrep.executeUpdate(EdbcPrep.java:442)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:464)
... 8 more
Name and version of the database you are using:
IngresII 2.6
The generated SQL (show_sql=true):
Hibernate: insert into ddwindow (nweek, study, daset) values (?, ?, ?)
Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 15, 2005 2:11 am 
Beginner
Beginner

Joined: Fri Feb 11, 2005 12:03 pm
Posts: 48
Location: Kiel, Germany
Doesn't this part of the stacktrace
Code:
Caused by: ca.edbc.util.EdbcEx: Duplicate key on INSERT detected.

indicate that it is merely a database issue and not related to hibernate in the first place?

Is the primary key on your table correct?
Did you test the insert statement using another client?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 15, 2005 8:41 am 
Beginner
Beginner

Joined: Fri Feb 04, 2005 2:44 pm
Posts: 25
Location: Buffalo, NY
The primary keys are correct.

I found the problem. The size of the table column that Hibernate is saving into is 8 characters. The values of the Java String objects were longer than 8 chars, and were not unique within the first 8 chars. So they were duplicates after being truncated.

That raises another question. I mean Java Strings can be really big, so how can one know how big to make table columns?


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.