This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

bidirectional one to many mapping issues

 
patrick patel
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic