aspose file tools*
The moose likes Object Relational Mapping and the fly likes ConstraintViolationException in Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "ConstraintViolationException in Hibernate" Watch "ConstraintViolationException in Hibernate" New topic
Author

ConstraintViolationException in Hibernate

Haricharan Maramraj
Greenhorn

Joined: Dec 19, 2004
Posts: 6
Hi Everyone:

I have been struggling to find the root cause of the following error in my application:

could not insert: [com.averitt.DataLayout#1000]
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 com.averitt.EventManager.testCreateAndRead(EventManager.java:294)
at com.averitt.EventManager.setUp(EventManager.java:195)
at com.averitt.EventManager.main(EventManager.java:307)
Caused by: com.ibm.db2.jcc.b.SqlException: DB2 SQL error: SQLCODE: -302, SQLSTATE: 22001, SQLERRMC: null

My Table(DataLayout) in the backend looks:

create table db2admin.datalayout(datalayoutid bigint not null primary key, xslfo varchar(30))

My mapping file looks:

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

<hibernate-mapping>
<class name="com.averitt.DataLayout" table="datalayout">
<id name="id" column="datalayoutid" type="long">
<generator class="assigned">
</generator>
</id>

<property name="xslFO" column="xslfo" type="string"/>
</class>

</hibernate-mapping>
<!-- parsed in 10ms -->

My application code looks:

DataLayout layout = new DataLayout();
Long lon = new Long(1000);
layout.setId(lon.longValue());
layout.setXslFO("Extensible Style Sheet Language Formatting Objects");
------------------------
} else if (Class.forName("com.averitt.DataLayout").isInstance(anObject)) {
sess = sessions.openSession();
tx = sess.beginTransaction();

sess.save((DataLayout)anObject);

Could anyone have any idea what's going wrong in my code? I also made sure that no duplicate keys nor null values were set to the above object. I am baffled with this error. I appreciate your help.


Thankyou
Haricharan M.
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
looks like the same error (302/22001) as in your other message.
- did you check your mappings and your dialect settings and your table definition ?

some questions and remarks
- why do you cast anObject to DataLayout before calling save ?
- why do you use isInstance() instead of instanceof operator ?
- why do you create a Long first and then call longValue() on it ? try to use nullable values as identifiers with hibernate (also see the hibernate doco)

pascal
 
Consider Paul's rocket mass heater.
 
subject: ConstraintViolationException in Hibernate