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

bidirectional one to many mapping issues

patrick patel
Greenhorn

Joined: Jul 15, 2005
Posts: 5
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
necati sekkeli
Greenhorn

Joined: Jun 21, 2004
Posts: 26
<class name="Customer" table="Customer">
<many-to-one name="adress" column="adress_id" class=". . .adress" cascade="save-update"/>

</class>

can be a solution. Cascade can be the problem. You can try to add cascade property to your customer xml.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: bidirectional one to many mapping issues
 
Similar Threads
Issue in Inserting data using One to Many
Retereive data by ORDER BY
[Hibernate] Composite key one-to-many XML Error
Hibernate Issue
One-to-many mapping and composite key