-->
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.  [ 6 posts ] 
Author Message
 Post subject: Simple one-to-many mapping = Not so simple
PostPosted: Wed Nov 17, 2004 3:05 pm 
Newbie

Joined: Wed Nov 17, 2004 1:16 pm
Posts: 6
Location: Charlottesville, VA
Okay, other than everything I added below I'm not sure what else I can say...

Basically, I have an object that contains a HashSet() of other objects... which on the Java side represents my one-to-many relationship...

If I only have one object in the HashSet() then I can persist to the database all day... If HashSet().getSize() > 1... I get everything posted below...

I've tried various different things but have been at a standstill for a couple of days... Any and all help greatly appreciated...

Hibernate version: 2.1

Mapping documents:
PERSON:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping package="com.justin.learn">
<class name="Person" table="TBL_PERSON">
<id name="personID" column="GUID" type="int">
<generator class="increment"/>
</id>
<property name="personType" column="PERSON_TYPE"
type="string"/>
<property name="personName" column="PERSON_NAME"
type="string"/>
<set name="numbers"
inverse="true"
cascade="all-delete-orphan">
<key column="PERSON_ID"/>
<one-to-many class="Number"/>
</set>
<set name="addresses"
inverse="true"
cascade="all-delete-orphan">
<key column="PERSON_ID"/>
<one-to-many class="Address"/>
</set>
</class>
</hibernate-mapping>

NUMBER:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping package="com.justin.learn">
<class name="Number" table="TBL_NUMBER">
<id name="numberID" column="GUID" type="int">
<generator class="increment"/>
</id>
<property name="numberType" column="NUMBER_TYPE"
type="string"/>
<property name="areaCode" column="AREA_CODE" type="int"/>
<property name="phoneNumber" column="PHONE_NUMBER" type="int"/>
<many-to-one
name="person"
column="PERSON_ID"
class="Person"
not-null="true"/>
</class>
</hibernate-mapping>

ADDRESS:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping package="com.justin.learn">
<class name="Address" table="TBL_ADDRESS">
<id name="addressID" column="GUID" type="int">
<generator class="increment"/>
</id>
<property name="addressType" column="ADDRESS_TYPE"
type="string"/>
<property name="street" column="STREET" type="string"/>
<property name="city" column="CITY" type="string"/>
<property name="state" column="STATE" type="string"/>
<property name="zip" column="ZIP" type="int"/>
<many-to-one
name="person"
column="PERSON_ID"
class="Person"
not-null="true"/>
</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
public class Driver implements Serializable {
static Logger logger = Logger.getLogger(Driver.class);

public static void main(String args[]) throws Exception {
BasicConfigurator.configure();

Configuration cfg = new Configuration();
cfg.addClass(Person.class);
cfg.addClass(Number.class);
cfg.addClass(Address.class);
// Hibern8IDE.startWith(cfg);

cfg.setProperty("hibernate.show_sql", "true");

SessionFactory sf = cfg.buildSessionFactory();
Session session = sf.openSession();

Person newPerson = null;
Number newNumber = null;
Address newAddress = null;

String inputFile = args[0];
String token = null;

System.out.println("Reading File...");
FileReader fr = new FileReader(inputFile);
BufferedReader br = new BufferedReader(fr);

String lineData = null;

while ((lineData = br.readLine()) != null) {
lineData = lineData.trim();
StringTokenizer st = new StringTokenizer(lineData);
if (st.hasMoreTokens()) {
token = st.nextToken(";").toLowerCase();
if (token.equals("person")) {
if (newPerson != null) {
session.save(newPerson);
newPerson = null;
}
newPerson = createNewPerson(st, newPerson);
session.save(newPerson);
} else if (token.equals("number")) {
if (newNumber != null) {
session.save(newNumber);
newNumber = null;
}
newNumber = createNewNumber(st, newNumber);
newPerson.setNumber(newNumber);
session.save(newNumber);
} else if (token.equals("address")) {
if (newAddress != null) {
session.save(newAddress);
newAddress = null;
}
newAddress = createNewAddress(st, newAddress);
newPerson.setAddress(newAddress);
session.save(newAddress);
}
}
}
session.save(newPerson);
session.save(newNumber);
session.save(newAddress);

session.flush();
session.connection().commit();
session.close();
}

public static Person createNewPerson(StringTokenizer st, Person newPerson) {
newPerson = new Person();
newPerson.setPersonType(st.nextToken());
newPerson.setPersonName(st.nextToken());
return newPerson;
}

public static Number createNewNumber(StringTokenizer st, Number newNumber) {
newNumber = new Number();
newNumber.setNumberType(st.nextToken());
newNumber.setAreaCode(Integer.parseInt(st.nextToken()));
newNumber.setPhoneNumber(Integer.parseInt(st.nextToken()));
return newNumber;
}

public static Address createNewAddress(StringTokenizer st, Address newAddress) {
newAddress = new Address();
newAddress.setAddressType(st.nextToken());
newAddress.setStreet(st.nextToken());
newAddress.setCity(st.nextToken());
newAddress.setState(st.nextToken());
newAddress.setZip(Integer.parseInt(st.nextToken()));
return newAddress;
}
}


Full stack trace of any exception that occurs:
Exception in thread "main" net.sf.hibernate.JDBCException: could not insert: [com.justin.learn.Address#3]
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:478)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:442)
at net.sf.hibernate.impl.ScheduledInsertion.execute(ScheduledInsertion.java:29)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2418)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2371)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2240)
at com.justin.learn.Driver.main(Driver.java:86)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:78)
Caused by: java.sql.SQLException: Duplicate key or integrity constraint violation message from server: "Cannot add or update a child row: a foreign key constraint fails"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1997)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1167)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1278)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2247)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1772)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1619)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:468)
... 11 more

Process finished with exit code 1


Name and version of the database you are using:
MySQL v.4.1.7


The generated SQL (show_sql=true):
Reading File...
Hibernate: insert into TBL_PERSON (PERSON_TYPE, PERSON_NAME, GUID) values (?, ?, ?)
Hibernate: insert into TBL_ADDRESS (ADDRESS_TYPE, STREET, CITY, STATE, ZIP, PERSON_ID, GUID) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into TBL_NUMBER (NUMBER_TYPE, AREA_CODE, PHONE_NUMBER, PERSON_ID, GUID) values (?, ?, ?, ?, ?)
Hibernate: insert into TBL_PERSON (PERSON_TYPE, PERSON_NAME, GUID) values (?, ?, ?)
Hibernate: insert into TBL_ADDRESS (ADDRESS_TYPE, STREET, CITY, STATE, ZIP, PERSON_ID, GUID) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into TBL_ADDRESS (ADDRESS_TYPE, STREET, CITY, STATE, ZIP, PERSON_ID, GUID) values (?, ?, ?, ?, ?, ?, ?)

OR

6400 [main] DEBUG net.sf.hibernate.impl.Printer - listing entities:
6410 [main] DEBUG net.sf.hibernate.impl.Printer - com.justin.learn.Address{addressID=7, street=7900 Harkins Road, person=Person#4, state=MD, addressType=Work, zip=20706, city=Lanham}
6410 [main] DEBUG net.sf.hibernate.impl.Printer - com.justin.learn.Number{numberType=Work, areaCode=301, numberID=3, person=Person#2, phoneNumber=4297526}
6410 [main] DEBUG net.sf.hibernate.impl.Printer - com.justin.learn.Address{addressID=6, street=410 Yantz Drive, person=Person#4, state=MD, addressType=Home, zip=21146, city=Severna Park}
6410 [main] DEBUG net.sf.hibernate.impl.Printer - com.justin.learn.Person{addresses=[Address#6, Address#7], numbers=[Number#9, Number#8, Number#7], personName=Michael Beck, personID=4, personType=Family}
6410 [main] DEBUG net.sf.hibernate.impl.Printer - com.justin.learn.Number{numberType=Home, areaCode=410, numberID=7, person=Person#4, phoneNumber=4215478}
6420 [main] DEBUG net.sf.hibernate.impl.Printer - com.justin.learn.Person{addresses=[Address#1], numbers=[Number#1], personName=Rodney vanZyl, personID=1, personType=Friend}
6420 [main] DEBUG net.sf.hibernate.impl.Printer - com.justin.learn.Number{numberType=Cell, areaCode=434, numberID=6, person=Person#3, phoneNumber=9061494}
6420 [main] DEBUG net.sf.hibernate.impl.Printer - com.justin.learn.Address{addressID=5, street=2120 Drovers Lane, person=Person#3, state=VA, addressType=Home, zip=22959, city=North Garden}
6420 [main] DEBUG net.sf.hibernate.impl.Printer - com.justin.learn.Person{addresses=[Address#5], numbers=[Number#6, Number#5], personName=Sarah Beck, personID=3, personType=Family}
6420 [main] DEBUG net.sf.hibernate.impl.Printer - com.justin.learn.Address{addressID=2, street=2120 Drovers Lane, person=Person#2, state=VA, addressType=Home, zip=22959, city=North Garden}
6430 [main] DEBUG net.sf.hibernate.impl.Printer - com.justin.learn.Number{numberType=Home, areaCode=434, numberID=5, person=Person#3, phoneNumber=9230524}
6430 [main] DEBUG net.sf.hibernate.impl.Printer - com.justin.learn.Address{addressID=8, street=410 Yantz Drive, person=Person#5, state=MD, addressType=Home, zip=21146, city=Severna Park}
6430 [main] DEBUG net.sf.hibernate.impl.Printer - com.justin.learn.Person{addresses=[Address#4, Address#3, Address#2], numbers=[Number#2, Number#4, Number#3], personName=Justin Beck, personID=2, personType=Family}
6430 [main] DEBUG net.sf.hibernate.impl.Printer - com.justin.learn.Number{numberType=Cell, areaCode=410, numberID=9, person=Person#4, phoneNumber=5626123}
6430 [main] DEBUG net.sf.hibernate.impl.Printer - com.justin.learn.Number{numberType=Cell, areaCode=434, numberID=4, person=Person#2, phoneNumber=9061493}
6440 [main] DEBUG net.sf.hibernate.impl.Printer - com.justin.learn.Number{numberType=Home, areaCode=410, numberID=10, person=Person#5, phoneNumber=4215478}
6440 [main] DEBUG net.sf.hibernate.impl.Printer - com.justin.learn.Person{addresses=[Address#8], numbers=[Number#10], personName=Maureen Beck, personID=5, personType=Family}
6440 [main] DEBUG net.sf.hibernate.impl.Printer - com.justin.learn.Address{addressID=3, street=7900 Harkins Road, person=Person#2, state=MD, addressType=Work, zip=20706, city=Lanham}
6440 [main] DEBUG net.sf.hibernate.impl.Printer - com.justin.learn.Number{numberType=Home, areaCode=434, numberID=2, person=Person#2, phoneNumber=9230524}
6440 [main] DEBUG net.sf.hibernate.impl.Printer - com.justin.learn.Number{numberType=Home, areaCode=434, numberID=1, person=Person#1, phoneNumber=9737633}
6440 [main] DEBUG net.sf.hibernate.impl.Printer - com.justin.learn.Number{numberType=Work, areaCode=301, numberID=8, person=Person#4, phoneNumber=4297043}
6440 [main] DEBUG net.sf.hibernate.impl.Printer - more......


Debug level Hibernate log excerpt:
Is this what you are looking for? Not sure about this:

6440 [main] DEBUG net.sf.hibernate.impl.SessionImpl - executing flush
6450 [main] DEBUG net.sf.ehcache.Cache - TBL_PERSON now: 1100717732565
6450 [main] DEBUG net.sf.ehcache.Cache - TBL_PERSON Creation Time: 1100717732565 Next To Last Access Time: 0
6450 [main] DEBUG net.sf.ehcache.Cache - TBL_PERSON mostRecentTime: 1100717732565
6450 [main] DEBUG net.sf.ehcache.Cache - TBL_PERSON Age to Idle: 120000 Age Idled: 0
6580 [main] DEBUG net.sf.ehcache.Cache - net.sf.hibernate.cache.UpdateTimestampsCache: Is element with key TBL_PERSON expired?: false
6580 [main] DEBUG net.sf.hibernate.persister.EntityPersister - Inserting entity: [com.justin.learn.Person#1]
6580 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - about to open: 0 open PreparedStatements, 0 open ResultSets
6580 [main] DEBUG net.sf.hibernate.SQL - insert into TBL_PERSON (PERSON_TYPE, PERSON_NAME, GUID) values (?, ?, ?)
Hibernate: insert into TBL_PERSON (PERSON_TYPE, PERSON_NAME, GUID) values (?, ?, ?)
6580 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - preparing statement
6590 [main] DEBUG net.sf.hibernate.persister.EntityPersister - Dehydrating entity: [com.justin.learn.Person#1]
6590 [main] DEBUG net.sf.hibernate.type.StringType - binding 'Friend' to parameter: 1
6590 [main] DEBUG net.sf.hibernate.type.StringType - binding 'Rodney vanZyl' to parameter: 2
6590 [main] DEBUG net.sf.hibernate.type.IntegerType - binding '1' to parameter: 3
6600 [main] DEBUG net.sf.ehcache.Cache - TBL_PERSON now: 1100717732715
6600 [main] DEBUG net.sf.ehcache.Cache - TBL_PERSON Creation Time: 1100717732565 Next To Last Access Time: 0
6600 [main] DEBUG net.sf.ehcache.Cache - TBL_PERSON mostRecentTime: 1100717732565
6600 [main] DEBUG net.sf.ehcache.Cache - TBL_PERSON Age to Idle: 120000 Age Idled: 150
6600 [main] DEBUG net.sf.ehcache.Cache - net.sf.hibernate.cache.UpdateTimestampsCache: Is element with key TBL_PERSON expired?: false
6600 [main] DEBUG net.sf.hibernate.persister.EntityPersister - Inserting entity: [com.justin.learn.Address#1]
6600 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - done closing: 0 open PreparedStatements, 0 open ResultSets
6600 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - closing statement
6610 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - about to open: 0 open PreparedStatements, 0 open ResultSets
6610 [main] DEBUG net.sf.hibernate.SQL - insert into TBL_ADDRESS (ADDRESS_TYPE, STREET, CITY, STATE, ZIP, PERSON_ID, GUID) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into TBL_ADDRESS (ADDRESS_TYPE, STREET, CITY, STATE, ZIP, PERSON_ID, GUID) values (?, ?, ?, ?, ?, ?, ?)
6610 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - preparing statement
6610 [main] DEBUG net.sf.hibernate.persister.EntityPersister - Dehydrating entity: [com.justin.learn.Address#1]
6610 [main] DEBUG net.sf.hibernate.type.StringType - binding 'Home' to parameter: 1
6630 [main] DEBUG net.sf.hibernate.type.StringType - binding '1234 Any Street' to parameter: 2
6630 [main] DEBUG net.sf.hibernate.type.StringType - binding 'Earlysville' to parameter: 3
6630 [main] DEBUG net.sf.hibernate.type.StringType - binding 'VA' to parameter: 4
6630 [main] DEBUG net.sf.hibernate.type.IntegerType - binding '22911' to parameter: 5
6630 [main] DEBUG net.sf.hibernate.type.IntegerType - binding '1' to parameter: 6
6630 [main] DEBUG net.sf.hibernate.type.IntegerType - binding '1' to parameter: 7
6640 [main] DEBUG net.sf.ehcache.Cache - TBL_PERSON now: 1100717732755
6640 [main] DEBUG net.sf.ehcache.Cache - TBL_PERSON Creation Time: 1100717732565 Next To Last Access Time: 0
6640 [main] DEBUG net.sf.ehcache.Cache - TBL_PERSON mostRecentTime: 1100717732565
6640 [main] DEBUG net.sf.ehcache.Cache - TBL_PERSON Age to Idle: 120000 Age Idled: 190
6640 [main] DEBUG net.sf.ehcache.Cache - net.sf.hibernate.cache.UpdateTimestampsCache: Is element with key TBL_PERSON expired?: false
6640 [main] DEBUG net.sf.hibernate.persister.EntityPersister - Inserting entity: [com.justin.learn.Number#1]
6640 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - done closing: 0 open PreparedStatements, 0 open ResultSets
6640 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - closing statement
6640 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - about to open: 0 open PreparedStatements, 0 open ResultSets
6640 [main] DEBUG net.sf.hibernate.SQL - insert into TBL_NUMBER (NUMBER_TYPE, AREA_CODE, PHONE_NUMBER, PERSON_ID, GUID) values (?, ?, ?, ?, ?)
Hibernate: insert into TBL_NUMBER (NUMBER_TYPE, AREA_CODE, PHONE_NUMBER, PERSON_ID, GUID) values (?, ?, ?, ?, ?)
6650 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - preparing statement
6650 [main] DEBUG net.sf.hibernate.persister.EntityPersister - Dehydrating entity: [com.justin.learn.Number#1]
6650 [main] DEBUG net.sf.hibernate.type.StringType - binding 'Home' to parameter: 1
6650 [main] DEBUG net.sf.hibernate.type.IntegerType - binding '434' to parameter: 2
6650 [main] DEBUG net.sf.hibernate.type.IntegerType - binding '9737633' to parameter: 3
6650 [main] DEBUG net.sf.hibernate.type.IntegerType - binding '1' to parameter: 4
6650 [main] DEBUG net.sf.hibernate.type.IntegerType - binding '1' to parameter: 5
6650 [main] DEBUG net.sf.hibernate.persister.EntityPersister - Inserting entity: [com.justin.learn.Person#2]
6650 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - done closing: 0 open PreparedStatements, 0 open ResultSets
6660 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - closing statement
6660 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - about to open: 0 open PreparedStatements, 0 open ResultSets
6660 [main] DEBUG net.sf.hibernate.SQL - insert into TBL_PERSON (PERSON_TYPE, PERSON_NAME, GUID) values (?, ?, ?)
Hibernate: insert into TBL_PERSON (PERSON_TYPE, PERSON_NAME, GUID) values (?, ?, ?)
6660 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - preparing statement
6660 [main] DEBUG net.sf.hibernate.persister.EntityPersister - Dehydrating entity: [com.justin.learn.Person#2]
6660 [main] DEBUG net.sf.hibernate.type.StringType - binding 'Family' to parameter: 1
6660 [main] DEBUG net.sf.hibernate.type.StringType - binding 'Justin Beck' to parameter: 2
6660 [main] DEBUG net.sf.hibernate.type.IntegerType - binding '2' to parameter: 3
6670 [main] DEBUG net.sf.hibernate.persister.EntityPersister - Inserting entity: [com.justin.learn.Address#2]
6670 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - done closing: 0 open PreparedStatements, 0 open ResultSets
6670 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - closing statement
6670 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - about to open: 0 open PreparedStatements, 0 open ResultSets
6670 [main] DEBUG net.sf.hibernate.SQL - insert into TBL_ADDRESS (ADDRESS_TYPE, STREET, CITY, STATE, ZIP, PERSON_ID, GUID) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into TBL_ADDRESS (ADDRESS_TYPE, STREET, CITY, STATE, ZIP, PERSON_ID, GUID) values (?, ?, ?, ?, ?, ?, ?)
6670 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - preparing statement
6670 [main] DEBUG net.sf.hibernate.persister.EntityPersister - Dehydrating entity: [com.justin.learn.Address#2]
6670 [main] DEBUG net.sf.hibernate.type.StringType - binding 'Home' to parameter: 1
6670 [main] DEBUG net.sf.hibernate.type.StringType - binding '2120 Drovers Lane' to parameter: 2
6670 [main] DEBUG net.sf.hibernate.type.StringType - binding 'North Garden' to parameter: 3
6670 [main] DEBUG net.sf.hibernate.type.StringType - binding 'VA' to parameter: 4
6680 [main] DEBUG net.sf.hibernate.type.IntegerType - binding '22959' to parameter: 5
6680 [main] DEBUG net.sf.hibernate.type.IntegerType - binding '2' to parameter: 6
6680 [main] DEBUG net.sf.hibernate.type.IntegerType - binding '2' to parameter: 7
6680 [main] DEBUG net.sf.hibernate.persister.EntityPersister - Inserting entity: [com.justin.learn.Address#3]
6680 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - reusing prepared statement
6680 [main] DEBUG net.sf.hibernate.SQL - insert into TBL_ADDRESS (ADDRESS_TYPE, STREET, CITY, STATE, ZIP, PERSON_ID, GUID) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into TBL_ADDRESS (ADDRESS_TYPE, STREET, CITY, STATE, ZIP, PERSON_ID, GUID) values (?, ?, ?, ?, ?, ?, ?)
6680 [main] DEBUG net.sf.hibernate.persister.EntityPersister - Dehydrating entity: [com.justin.learn.Address#3]
6680 [main] DEBUG net.sf.hibernate.type.StringType - binding 'Work' to parameter: 1
6680 [main] DEBUG net.sf.hibernate.type.StringType - binding '7900 Harkins Road' to parameter: 2
6690 [main] DEBUG net.sf.hibernate.type.StringType - binding 'Lanham' to parameter: 3
6690 [main] DEBUG net.sf.hibernate.type.StringType - binding 'MD' to parameter: 4
6690 [main] DEBUG net.sf.hibernate.type.IntegerType - binding '20706' to parameter: 5
6690 [main] DEBUG net.sf.hibernate.type.IntegerType - binding '2' to parameter: 6
6690 [main] DEBUG net.sf.hibernate.type.IntegerType - binding '3' to parameter: 7
6690 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - done closing: 0 open PreparedStatements, 0 open ResultSets
6690 [main] DEBUG net.sf.hibernate.impl.BatcherImpl - closing statement
6740 [main] DEBUG net.sf.hibernate.util.JDBCExceptionReporter - SQL Exception
java.sql.SQLException: Duplicate key or integrity constraint violation message from server: "Cannot add or update a child row: a foreign key constraint fails"

_________________
"If you choose not to decide you still have made a choice." - Niel Peart


Top
 Profile  
 
 Post subject: Re: Simple one-to-many mapping = Not so simple
PostPosted: Wed Nov 17, 2004 3:15 pm 
Newbie

Joined: Thu Sep 30, 2004 12:50 am
Posts: 2
Location: Louvain-La-Neuve, Belgium
the many-to-one relationship of address to person has an attribute "not-null" equals to true, but I do not think you did a address.setPerson().

Read the chapter 16 regarding parent/child associations, if I understand well it's exactly what you want to do.

HTH,

Julien


Top
 Profile  
 
 Post subject: Re: Simple one-to-many mapping = Not so simple
PostPosted: Wed Nov 17, 2004 4:12 pm 
Newbie

Joined: Wed Nov 17, 2004 1:16 pm
Posts: 6
Location: Charlottesville, VA
Hi, thanks for posting... I've looked in the book "Hibernate in Action", and frankly I'm still pretty confused (not through any fault of the book)... Can anyone look at my mapping files and see an obvious mistake? I'll keep hunting the answer down myself but I feel like I'm going in circles...

Thanks!

_________________
"If you choose not to decide you still have made a choice." - Niel Peart


Top
 Profile  
 
 Post subject: Re: Simple one-to-many mapping = Not so simple
PostPosted: Wed Nov 17, 2004 11:08 pm 
Newbie

Joined: Wed Nov 17, 2004 1:16 pm
Posts: 6
Location: Charlottesville, VA
Aha! FIXED!!!

_________________
"If you choose not to decide you still have made a choice." - Niel Peart


Top
 Profile  
 
 Post subject: How I fixed it...
PostPosted: Thu Nov 18, 2004 10:54 am 
Newbie

Joined: Wed Nov 17, 2004 1:16 pm
Posts: 6
Location: Charlottesville, VA
Hi,

It turned out to be a 4 part answer as follows:

1) I thought it odd that I was setting cascade="all" in the sets of my person.hbm.xnml file but I was still having to physically persist the "sub-objects" (numbers and addresses) to the database by using a session.save() every time. So, I stopped doing that (that just seemed to make sense).

2) I noticed that once I had done step 1, that my application was trying to persist my sub-objects to the database with an ID of '0' every time. So I added "unsaved-value="0"" to the ID tags in the mapping files for my number and address objects... (see page 138 section 4.3.4 in HIA for the correct explanation for this).

3) Once I had done step 2, I noticed that my application could populate all the numbers but could not populate the addresses. Since they are fundamentally the same I printed out the mapping files and classes for each and did a side by side comparison checking all the attributes and accessor methods as I went. I found that in my Address mapping file I was referencing TBL_NUMBER instead of TBL_ADDRESS... So I corrected that and tested it again.

4) Step three didn't seem to help although it was an obvious error. The only thing I could think of (since I was getting foreign key constraint violations when I tried to populate addresses) was that there might be a problem with the database itself. So, I dropped the schema I was writing to and built a new one using the DDL I had developed (after checking the DDL, of course)... And PRESTO!!! It worked. I can only imagine that while poking around in the database I had somehow changed the behavior of TBL_ADDRESS.fk_tbl_address!

So there you have it.

Cheers,

Justin

_________________
"If you choose not to decide you still have made a choice." - Niel Peart


Top
 Profile  
 
 Post subject: How I fixed it...
PostPosted: Thu Nov 18, 2004 10:56 am 
Newbie

Joined: Wed Nov 17, 2004 1:16 pm
Posts: 6
Location: Charlottesville, VA
Hi,

It turned out to be a 4 part answer as follows:

1) I thought it odd that I was setting cascade="all" in the sets of my person.hbm.xnml file but I was still having to physically persist the "sub-objects" (numbers and addresses) to the database by using a session.save() every time. So, I stopped doing that (that just seemed to make sense).

2) I noticed that once I had done step 1, that my application was trying to persist my sub-objects to the database with an ID of '0' every time. So I added "unsaved-value="0"" to the ID tags in the mapping files for my number and address objects... (see page 138 section 4.3.4 in HIA for the correct explanation for this).

3) Once I had done step 2, I noticed that my application could populate all the numbers but could not populate the addresses. Since they are fundamentally the same I printed out the mapping files and classes for each and did a side by side comparison checking all the attributes and accessor methods as I went. I found that in my Address mapping file I was referencing TBL_NUMBER instead of TBL_ADDRESS... So I corrected that and tested it again.

4) Step three didn't seem to help although it was an obvious error. The only thing I could think of (since I was getting foreign key constraint violations when I tried to populate addresses) was that there might be a problem with the database itself. So, I dropped the schema I was writing to and built a new one using the DDL I had developed (after checking the DDL, of course)... And PRESTO!!! It worked. I can only imagine that while poking around in the database I had somehow changed the behavior of TBL_ADDRESS.fk_tbl_address!

So there you have it.

Cheers,

Justin

_________________
"If you choose not to decide you still have made a choice." - Niel Peart


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.