-->
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.  [ 2 posts ] 
Author Message
 Post subject: bidirectional one to many mapping issues
PostPosted: Mon Sep 12, 2005 4:57 pm 
Newbie

Joined: Mon Sep 12, 2005 4:51 pm
Posts: 1
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
Hibernate 3.0.5

Mapping documents:
customer.hbm.xml, address.hbm.xml

Code between sessionFactory.openSession() and session.close():
Object obj = addcust;
session.save(obj);
session.connection().commit();

Full stack trace of any exception that occurs:
None

Name and version of the database you are using:
MySQL 4.1.12

The generated SQL (show_sql=true):
Hibernate: insert into customers (first_name, last_name) values (?, ?)

Debug level Hibernate log excerpt:


Hi,

I am trying to a object to database using Hibernate. There are basically two objects viz. A 'customer' having one or many 'addresses'. The customer object stores the details of associated addresses using a Set. I am able to store the details of customer objects but the details of associated addresses are lost in the transaction and are never stored in the database. The system does not give out any errors. I refered to many tutorials for bidirectional one to many mapping but none of them work for me.

Some section of code is as follows:

-------------
Customer.java
-------------
package src;

import java.util.Set;
import java.util.HashSet;

public class Customer {
private long custid;
private String fname;
private String lname;
private Set addresses = new HashSet();
..........................
}

--------------
Address.java
--------------
package src;

import java.util.Set;
import java.util.HashSet;

public class Address {
private long addid;
private String address;
private String city;
private src.Customer customer;
.......................
}

----------------
Customer.hbm.xml
----------------
<?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="src.Customer" table="customers">
<id name="custid" column="custid" type="long" unsaved-value="null">
<generator class="native"/>
</id>
<property name="fname" column="first_name" type="string" length="30" not-null="true"/>
<property name="lname" column="last_name" type="string" length="30" not-null="true"/>
<set name="addresses" cascade="all" inverse="true" lazy="true">
<key column="custid"/>
<one-to-many class="src.Address"/>
</set>
</class>
</hibernate-mapping>

------------------
Address.hbm.xml
------------------
<?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="src.Address" table="addresses">
<id name="addid" column="addid" type="long" unsaved-value="null">
<generator class="native"/>
</id>
<property name="address" column="address" type="string" length="30" not-null="true"/>
<property name="city" column="city" type="string" length="30" not-null="true"/>
<many-to-one name="customer" class="src.Customer" column="custid"/>
</class>
</hibernate-mapping>

------------------------
Adding customer details
------------------------

Customer addcust = new Customer();
addcust.setFname(ipform.getFname());
addcust.setLname(ipform.getLname());

Address addaddress = new Address();
addaddress.setAddress(ipform.getAddress());
addaddress.setCity(ipform.getCity());
addaddress.setCustomer(addcust);

Set firstadd = new HashSet();
firstadd.add(addaddress);

addcust.setAddresses(firstadd);

Session session = null;
try {

session = HibernateSessionFactory.currentSession();

Object obj = addcust;
session.save(obj);
session.connection().commit();
session.close();

..............

I tried many different tutorials. Still I am only able to store the customer details in the database. The associated address details cannot be store into the database.

Can anyone please indicate as to what is the possible configuration error.

Thanks,
Patrick


Top
 Profile  
 
 Post subject: Re: bidirectional one to many mapping issues
PostPosted: Mon Sep 12, 2005 5:18 pm 
Regular
Regular

Joined: Thu Dec 11, 2003 4:14 pm
Posts: 86
Location: Hibernate 3 + annotations, Oracle 9i, PostgreSQL 8.0, Java 1.5.0
pratikgpatel wrote:
<set name="addresses" cascade="all" inverse="true" lazy="true">


As in http://www.hibernate.org/hib_docs/v3/reference/en/html_single/ Section 2.3.6 try to put the inverse attribute on the other side.

Hope this helps.


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