wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes persisting a collection - one-to-many mapping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "persisting a collection - one-to-many mapping" Watch "persisting a collection - one-to-many mapping" New topic
Author

persisting a collection - one-to-many mapping

sai kinnera
Ranch Hand

Joined: Aug 12, 2004
Posts: 52
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

Joined: Feb 05, 2001
Posts: 17249
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Rashid Darvesh
Ranch Hand

Joined: Feb 13, 2004
Posts: 189
Have a look at this use cases.
They are very indepth.

Rashid
Rashid Darvesh
Ranch Hand

Joined: Feb 13, 2004
Posts: 189
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

Joined: Feb 05, 2001
Posts: 17249
    
    6

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: persisting a collection - one-to-many mapping
 
Similar Threads
Exception when having different class and table name
Hibernate throwing SAXPasesExceptions
If somebody can translate this for me please?
Help with mapping set - Hibernate
Hibernate 3 Not Loading Values Into Child Set In JUnit Test