-->
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: org.hibernate.exception.ConstraintViolationException
PostPosted: Sun Aug 14, 2011 4:35 am 
Newbie

Joined: Sun Jul 10, 2011 11:16 am
Posts: 6
i am making a sample program using this link:
http://www.vaannila.com/hibernate/hiber ... one-1.html

i have following tables in sqlserver:
Code:
CREATE TABLE Student2
(
STUDENT_ID int NOT NULL,
LastName varchar(255) NOT NULL,
STUDENT_NAME varchar(255),
STUDENT_ADDRESS varchar(255),
UNIQUE (STUDENT_ADDRESS)
)
====
CREATE TABLE Address2
(
ADDRESS_ID int NOT NULL,
LastName varchar(255) NOT NULL,
ADDRESS_STREET varchar(255),
ADDRESS_CITY varchar(255),
ADDRESS_STATE varchar(255),
ADDRESS_ZIPCODE varchar(255),
)
====



i have take studentId type int in Student.hbm.xml:
Code:
<id name="studentId" type="int" column="STUDENT_ID">
            <generator class="native" />
        </id>



i have take addressId type int in Address.hbm.xml:
Code:
<id name="addressId" type="int" column="ADDRESS_ID">
         <generator class="native" />
      </id>


i am facing following exception:
Code:
org.hibernate.exception.ConstraintViolationException: could not insert: [com.vaannila.student.Address]
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:40)
   at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2158)
   at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2638)
   at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:48)
   at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
   at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:298)
   at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
   at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:94)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
   at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
   at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
   at org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:218)
   at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
   at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
   at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
   at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
   at org.hibernate.event.def.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:431)
   at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:265)
   at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
   at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
   at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
   at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
   at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
   at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
   at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
   at com.vaannila.student.Main.main(Main.java:24)
Caused by: java.sql.SQLException: Cannot insert the value NULL into column 'ADDRESS_ID', table 'sample.dbo.Address2'; column does not allow nulls. INSERT fails.
   at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:368)
   at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2820)
   at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2258)
   at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:632)
   at net.sourceforge.jtds.jdbc.JtdsStatement.processResults(JtdsStatement.java:584)
   at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQL(JtdsStatement.java:546)
   at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeUpdate(JtdsPreparedStatement.java:504)
   at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73)
   at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33)
   ... 31 more


please help.


Top
 Profile  
 
 Post subject: Re: org.hibernate.exception.ConstraintViolationException
PostPosted: Fri Oct 24, 2014 5:02 am 
Newbie

Joined: Fri Oct 24, 2014 4:54 am
Posts: 2
hi,
i got the same exception
i think the problem is here

<id name="studentId" type="int" column="STUDENT_ID">
<generator class="native" />
</id>


i've tried to do this without the generator property and it works
<id name="studentId" type="int" column="STUDENT_ID">
</id>


All the best
;)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 24, 2014 5:16 am 
Newbie

Joined: Fri Oct 24, 2014 4:54 am
Posts: 2
for more information

http://www.roseindia.net/hibernate/hibernateidgeneratorelement.shtml


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.