This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

bidirectional one to many mapping issues

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic