-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: many-to-one column mapping
PostPosted: Mon Feb 19, 2007 1:12 am 
Newbie

Joined: Mon Feb 19, 2007 12:54 am
Posts: 16
Location: banglore, india
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.