-->
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.  [ 35 posts ]  Go to page 1, 2, 3  Next
Author Message
 Post subject: Problem saving data with link table relationship...
PostPosted: Tue Apr 24, 2007 2:10 pm 
Beginner
Beginner

Joined: Tue Apr 24, 2007 2:03 pm
Posts: 23
I'm using Hibernate 3.x and created a fairly straight forward relationship. I have CUSTOMER, ADDRESS and LINK_CUST_ADDR tables. The LINK_CUST_ADDR table just defines a foreign key relationship to each of the other two tables so I can have a many-to-many relationship.

I am using MyEclipseIDE's built in support for generation of the POJOs. They seem to have been generated correctly from what I can tell.

I wrote a test case where I created a Customer object with one Address, then saved and committed the transaction. When I run the test it appears to succeed (no errors) but when I check the database I see the customer, but no entry in the the address table or link_cust_addr.

I imagined that I could create not only a new customer but a new address and have hibernate automatically create the entry in all the tables including the link table.

What am I missing?

Pneu


Top
 Profile  
 
 Post subject: Re: Problem saving data with link table relationship...
PostPosted: Tue Apr 24, 2007 2:37 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
pneudecorb wrote:
What am I missing?

Mapping files, POJO, persistence code.


Top
 Profile  
 
 Post subject: Associated Files...
PostPosted: Tue Apr 24, 2007 2:46 pm 
Beginner
Beginner

Joined: Tue Apr 24, 2007 2:03 pm
Posts: 23
I need to turn on logging to see the SQL that is generated. I haven't done that yet.

Here is the hibernate configuration:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

<session-factory>
<property name="myeclipse.connection.profile">
MySQL_Reservation
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/reservation
</property>
<property name="connection.username">root</property>
<property name="connection.password">xxxxxx</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<mapping resource="com/receptrix/hibernate/Address.hbm.xml" />
<mapping resource="com/receptrix/hibernate/Customer.hbm.xml" />
</session-factory>

</hibernate-configuration>

Here are the mapping files: Address first

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.receptrix.hibernate.Address" table="address" catalog="reservation">
<id name="addrId" type="java.lang.Integer">
<column name="addr_id" />
<generator class="native" />
</id>
<property name="addrLine1" type="java.lang.String">
<column name="addr_line_1" length="50" not-null="true" />
</property>
<property name="addrLine2" type="java.lang.String">
<column name="addr_line_2" length="50" />
</property>
<property name="city" type="java.lang.String">
<column name="city" length="50" not-null="true" />
</property>
<property name="stateProv" type="java.lang.String">
<column name="stateProv" length="50" not-null="true" />
</property>
<property name="country" type="java.lang.String">
<column name="country" length="2" not-null="true" />
</property>
<property name="zipcode" type="java.lang.Integer">
<column name="zipcode" not-null="true" />
</property>
<property name="zipext" type="java.lang.Integer">
<column name="zipext" />
</property>
<set name="customers" inverse="true" table="link_cust_addr" catalog="reservation">
<key>
<column name="addr_id" not-null="true" />
</key>
<many-to-many entity-name="com.receptrix.hibernate.Customer">
<column name="cust_id" not-null="true" unique="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>

------------------------
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.receptrix.hibernate.Customer" table="customer" catalog="reservation">
<id name="custId" type="java.lang.Integer">
<column name="cust_id" />
<generator class="native" />
</id>
<property name="lastName" type="java.lang.String">
<column name="lastName" length="50" not-null="true" />
</property>
<property name="firstName" type="java.lang.String">
<column name="firstName" length="50" not-null="true" />
</property>
<property name="middleInit" type="java.lang.String">
<column name="middleInit" length="1" />
</property>
<property name="suffix" type="java.lang.String">
<column name="suffix" length="5" />
</property>
<property name="phone1" type="java.lang.String">
<column name="phone1" length="13"/>
</property>
<property name="phone1Type" type="java.lang.String">
<column name="phone1_type" length="10"/>
</property>
<property name="phone2" type="java.lang.String">
<column name="phone2" length="13" />
</property>
<property name="phone2Type" type="java.lang.String">
<column name="phone2_type" length="10"/>
</property>
<set name="addresses" table="link_cust_addr" catalog="reservation">
<key>
<column name="cust_id" not-null="true" unique="true" />
</key>
<many-to-many entity-name="com.receptrix.hibernate.Address">
<column name="addr_id" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>[/code][/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 24, 2007 3:43 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Mappings look fine from a cursory glance, can we see the code you are using to persist the data?


Top
 Profile  
 
 Post subject: Persistance Code...
PostPosted: Tue Apr 24, 2007 4:23 pm 
Beginner
Beginner

Joined: Tue Apr 24, 2007 2:03 pm
Posts: 23
I am using the DAO's generated by MyEclipseIDE's Hibernate Integration Tools. The actual calling sequence using these tools is pasted below.

After running the code below, the only table that shows a row inserted is the Customer Table. I removed all rows from all the tables before running the test below.

Thanks for your help. I really do appreciate the help.

@Test
public void testSave() {
CustomerDAO dao = new CustomerDAO();

Customer cust = new Customer();

Set<Address> addresses = cust.getAddresses();

Address addr = new Address();
addr.setAddrLine1("1020 East Harmon Road");
addr.setCity("Gilbert");
addr.setCountry("US");
addr.setStateProv("Arizona");
addr.setZipcode(21238); // not valid zip code...

addresses.add(addr);

cust.setAddresses(addresses);
cust.setFirstName("Mike");
cust.setLastName("Finnigan");
cust.setMiddleInit("J");
cust.setPhone1("4805551212");
cust.setPhone1Type("Home");

try {
dao.getSession().beginTransaction();
dao.save(cust);
dao.getSession().connection().commit();

} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
dao.getSession().close();
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 24, 2007 5:41 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
I believe it is here:

<set name="addresses" table="link_cust_addr" catalog="reservation">

try adding cascade="all" to this entry, e.g.

<set name="addresses" table="link_cust_addr" catalog="reservation" cascade="all">

Give it a shot and post results.


Top
 Profile  
 
 Post subject: Results from adding cascade="all"
PostPosted: Tue Apr 24, 2007 7:07 pm 
Beginner
Beginner

Joined: Tue Apr 24, 2007 2:03 pm
Posts: 23
I added the cascade attribute with a value of "all" as you suggested. After deleting the rows and rerunning the example, the customer and address rows were successfully added.

I would have expected that hibernate would have added a row to the link_cust_addr table for the association but didn't.

I only updated the customer mapping and didn't touch the address mapping.

Pneu


Top
 Profile  
 
 Post subject: Results from adding cascade="all"
PostPosted: Tue Apr 24, 2007 7:12 pm 
Beginner
Beginner

Joined: Tue Apr 24, 2007 2:03 pm
Posts: 23
I added the cascade attribute with a value of "all" as you suggested. After deleting the rows and rerunning the example, the customer and address rows were successfully added.

I would have expected that hibernate would have added a row to the link_cust_addr table for the association but didn't.

I only updated the customer mapping and didn't touch the address mapping.

Pneu


Top
 Profile  
 
 Post subject: Almost there...
PostPosted: Wed Apr 25, 2007 11:22 am 
Beginner
Beginner

Joined: Tue Apr 24, 2007 2:03 pm
Posts: 23
If I can just get the row added to the link table I should be good to go...I cannot imagine this has to be done manually.

I'll continue to review the Hibernate Docs and see what I can uncover on Many-To-Many mappings.

Pneu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 25, 2007 11:24 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
I am actually a bit baffled as to how it inserted into the two outer tables but not the link table. Can you turn on debug logging, perhaps pouring over the logs will give you some clue as to what is happening.


Top
 Profile  
 
 Post subject: Debugging enabled...
PostPosted: Wed Apr 25, 2007 12:12 pm 
Beginner
Beginner

Joined: Tue Apr 24, 2007 2:03 pm
Posts: 23
I enabled debugging but it really didn't tell me much. I can see the sections where the PreparedStatements are generated then executed on the customer and address insert statements, but no references to the link_cust_addr table.

Ideas?


Top
 Profile  
 
 Post subject: Refresh...
PostPosted: Thu Apr 26, 2007 11:00 am 
Beginner
Beginner

Joined: Tue Apr 24, 2007 2:03 pm
Posts: 23
Ananasi,

I wanted to refresh this thread and see if you had any more ideas.

Pneu


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 26, 2007 11:22 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Thanks, I did kind of lose track of this thread. I am not familiar with the experimental entity-name properties of the many-to-many elements. Can you change these to class instead and see what happens? We'll try to eliminate potential problems one at a time.


Top
 Profile  
 
 Post subject: Updated mappings...
PostPosted: Thu Apr 26, 2007 11:30 am 
Beginner
Beginner

Joined: Tue Apr 24, 2007 2:03 pm
Posts: 23
I moved the attribute name back to "class" as you suggested with the same results. Rows were inserted into the customer and address tables but not link_cust_addr.

Could it have something to do with how I defined the keys on the link_cust_addr table?

The table has the following structure:

column 1: cust_id (primary key)
column 2: addr_id (primary key)

Both of these are foreign keys into their respective counterparts, customer and address tables.

Seems like I am missing something fundamental.

Pneu


Top
 Profile  
 
 Post subject: Re: Updated mappings...
PostPosted: Thu Apr 26, 2007 11:41 am 
Newbie

Joined: Mon Apr 23, 2007 10:06 am
Posts: 6
Hi there

pneudecorb wrote:

Seems like I am missing something fundamental.



edit : my two cents would be that you have to save the address before saving the customer but i read this topic too quickly. excuse me.

_________________
Julien


Last edited by jchicoineau on Thu Apr 26, 2007 11:57 am, edited 1 time in total.

Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 35 posts ]  Go to page 1, 2, 3  Next

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.