Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ConstraintViolationException in Hibernate

Haricharan Maramraj
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)

  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic