File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

ConstraintViolationException in Hibernate

Haricharan Maramraj

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(
at net.sf.hibernate.exception.JDBCExceptionHelper.convert(
at net.sf.hibernate.persister.AbstractEntityPersister.convert(
at net.sf.hibernate.persister.EntityPersister.insert(
at net.sf.hibernate.persister.EntityPersister.insert(
at net.sf.hibernate.impl.ScheduledInsertion.execute(
at net.sf.hibernate.impl.SessionImpl.executeAll(
at net.sf.hibernate.impl.SessionImpl.execute(
at net.sf.hibernate.impl.SessionImpl.flush(
at com.averitt.EventManager.testCreateAndRead(
at com.averitt.EventManager.setUp(
at com.averitt.EventManager.main(
Caused by: 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">

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

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

<!-- parsed in 10ms -->

My application code looks:

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

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.

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)

I agree. Here's the link:
subject: ConstraintViolationException in Hibernate
It's not a secret anymore!