I have two tables
1. Supplier
2. Products.
CREATE TABLE supplier
( supplier_id numeric(10) not null,
supplier_name varchar2(50) not null,
contact_name varchar2(50),
CONSTRAINT supplier_pk PRIMARY KEY (supplier_id, supplier_name)
);
CREATE TABLE products
( product_id numeric(10) not null,
supplier_id numeric(10) not null,
supplier_name varchar2(50) not null,
CONSTRAINT fk_supplier_comp
FOREIGN KEY (supplier_id, supplier_name)
REFERENCES supplier(supplier_id, supplier_name)
);
Supplier Data
============
Supplier_ID | supplier_name | contact_name
------------- ----------------- ------------------
1 | Sreenath | Reddy
Products Data
=========
Product_id | supplier_ID | supplier_name
------------ ------------- -----------------
100 | 1 | Sreenath
Here supplier.hbm.xml
<hibernate-mapping>
<class name="bean.Supplier" table="supplier">
<composite-id>
<key-property name="supplierID" column="supplier_id"/>
<key-property name="supplierName" column="supplier_name"/>
</composite-id>
<property name="contactName" type="string">
<column name="contact_name"/>
</property>
<set name="productSet" inverse="true" cascade="all">
<key>
<column name="supplier_id"/>
<column name="supplier_name"/>
</key>
<one-to-many class="bean.Product"/>
</set>
</class>
</hibernate-mapping>
products.hbm.xml
<hibernate-mapping>
<class name="bean.Product" table="products">
<id name="productID" column="product_id">
<generator class="assigned"/>
</id>
<many-to-one name="supplier" class="bean.Supplier" not-null="true">
<column name="supplier_id" not-null="true"/>
<column name="supplier_name" not-null="true"/>
</many-to-one>
</class>
</hibernate-mapping>
Here is my Action class
tran = session.beginTransaction();
Supplier supp = new Supplier();
supp.setSupplierID(new Integer(1));
supp.setSupplierName("Sreenath");
supp =(Supplier) session.load(Supplier.class,supp);
Iterator iter = supp.getProductSet().iterator();
while(iter.hasNext()){
Product p =(Product) iter.next();
System.out.println("Product ID "+p.getProductID());
System.out.println("Supplier ID "+p.getSupplierID()); // it is null
System.out.println("Supplier Name "+p.getSupplierName());
}
while printing the supplierid and supplier name it is giving null, But I am getting the productid as 100.
May I know the reasong. Pls Its Urgent.
_________________ Banglore Developer
|