Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

persisting a collection - one-to-many mapping

 
sai kinnera
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I amgetting this exception when I try to persist an Address object which contains a list of Person objects. I took the id as assigned for both the objects. Still I am getting the violation of unique constraint exception as follows:
org.hibernate.exception.ConstraintViolationException: could not insert collection: [test.Address.persons#3001]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1058)



The mappings are:
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class
name="test.Person"
table="TEST_PERSON"
dynamic-update="false"
dynamic-insert="false">
<id
name="personId"
column="PERSON_ID"
type="java.lang.Integer">
<generator class="assigned"/>
</id>
<property
name="lastName"
type="java.lang.String"
update="true"
insert="true"
column="LAST_NM"/>
</class>
</hibernate-mapping>
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class
name="test.Address"

table="TEST_ADDRESS"
dynamic-update="false"
dynamic-insert="false">

<id
name="addressId"
column="ADDRESS_ID"
type="java.lang.Integer">
<generator class="assigned"/>
</id>
<property
name="street"
type="java.lang.String"
update="true"
insert="true"
column="STREET_ADDR"/>
<list name="persons" cascade="all">
<key column="PERSON_ID" />
<index column="index_nbr"/>
<one-to-many class="test.Person" />
</list>
</class>
</hibernate-mapping>

Can anybody tell me what mistake is being committed here. There is a list of Person objects in Address object.

Thanks in advance,
Sai
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sai, can you repost you mappings with CODE tags surround them so that the indentation of the xml remains. It is very difficult to read without those indentations. You will not be able to edit your message, because the < and > signs will be translated to < and >.

Mark
 
Rashid Darvesh
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at this use cases.
They are very indepth.

Rashid
 
Rashid Darvesh
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at this use cases.
They are very indepth.
Sorry forgot to add the link
http://www.makemyinfo.com/Tutorial.do?action=getTutorial&tutId=T0005.jsp
Rashid
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be a little careful with the link that Rashid posted, while most of it is a good example to see, there are a couple bad practices in there, including probably wanting to use try catches around your unit of works.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic