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