• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

persisting a collection - one-to-many mapping

 
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
 
ranger
Posts: 17346
11
Mac IntelliJ IDE 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
 
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
Posts: 17346
11
Mac IntelliJ IDE 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
 
Politics is a circus designed to distract you from what is really going on. So is this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic