My program is developed using Hibernate to demonstrate Collection mapping using
sets. Basically A product can contain many parts (set).
The basic probelm is when i run this program the products table is getting inserted but the parts table is empty.
Hibernate version: 3.2.4
Mapping documents
Product.hbm.xml
Code:
<?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="hibernate.Product" table="products">
<id name="productId" column="productId" >
</id>
<set name="parts">
<key column = "productId" />
<one-to-many class = "hibernate.Parts" />
</set>
<property name = "productName" type = "string" />
</class>
</hibernate-mapping>
Parts.hbm.xmlCode:
<?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="hibernate.Parts" table="parts">
<id name="partId" column="partId" >
<generator class="assigned"/>
</id>
<property name="partName" column="partName" />
</class>
</hibernate-mapping>
CodeCode:
package hibernate;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class ProductAppln {
public static void main(String[] args) {
Session session = null;
Transaction tx = null;
Product product = null;
Parts parts = null;
try{
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
tx = session.beginTransaction();
Set parts_set = new HashSet();
parts = new Parts();
parts.setPartId(1);
parts.setPartName("screw");
parts.setPartId(1);
parts_set.add(parts);
parts = new Parts();
parts.setPartId(2);
parts.setPartName("nuts");
parts_set.add(parts);
product = new Product();
product.setProductId(1);
product.setParts(parts_set);
product.setProductName("Laptop");
session.save(product);
tx.commit();
}catch(HibernateException e){
e.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}finally{
session.flush();
session.close();
System.exit(0);
}
}
}
Full stack trace of any exception that occurs:No error but this is what got displayed in consolellog4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: insert into products (productName, productId) values (?, ?)
Hibernate: update parts set productId=? where partId=?
Hibernate: update parts set productId=? where partId=?
Name and version of the database you are using: Oracle 9iConfiguration FileCode:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:bob</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.username">scott</property>
<property name="hibernate.connection.password">tiger</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="Parts.hbm.xml" />
<mapping resource="Product.hbm.xml" />
</session-factory>
</hibernate-configuration>
The Bean CodeCode:
package hibernate;
import java.util.Set;
public class Product {
private int productId;
private String productName;
private Set parts; //One product can contain many parts
public Set getParts() {
return parts;
}
public void setParts(Set parts) {
this.parts = parts;
}
public int getProductId() {
return productId;
}
public void setProductId(int productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
}
package hibernate;
public class Parts {
private int partId;
private String partName;
public int getPartId() {
return partId;
}
public void setPartId(int partId) {
this.partId = partId;
}
public String getPartName() {
return partName;
}
public void setPartName(String partName) {
this.partName = partName;
}
}
Results in the backend
SQL> select * from products;
PRODUCTID
----------
PRODUCTNAME
---------------------------------------------
1
Laptop
SQL> select * from parts;
no rows selected
NO DATA WAS BEING INSERTED in the PARTS TABLE