• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

composite-key one to many mapping problem

 
Narasimha Swami
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have three tables

Table:customer
--------------
customer_id String PRIMARY KEY
name String
address String

Table:products
--------------
product_id String PRIMARY KEY
name String

Table:customer_products
-----------------------
customer_id PRIMARY KEY
product_id PRIMARY KEY

Now in one-to-many mapping i fetch an Customer i must get
all records of customer_products related to customer_id of customer table.But I get only one record of customer_department per customer.Please help me in retrieving all the records of customer_products relating to customer_id of customer table. Here are POJOs and hbm files

customer.hbm.xml
<class class="pojo.Customer" table="customer">
<id column="customer_id" name="customerId" type="string">
<generator class="assigned"/>
</id>
.......
..........
...........
<set name="customerProducts" inverse="true">
<key column="customer_id"/>
<one-to-many class="pojo.CustomerProducts"/>
</set>
</class>

customerProducts.hbm.xml
<class class="pojo.CustomerProducts" table="customer">
<composite-id class="pojo.CustomerProductsPK" mapped="true">
<key-property column="customer_id" name="customerId"/>
<key-property column="product_id" name="productId"/>
<key-many-to-one name="customer" class="pojo.Customer"/>
</composite-id>
</class>

Customer.class an Serializable one

For ORM
Set customerProducts=new HashSet();
....... getter and setter of customerProducts.....

CustomerProducts.class an Serializable one

For ORM
Customer customer;
........ getter and setter of customer.....
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is this related to JSP? There's an ORM forum out.

Back to the actual problem: did you read the documentation that come along with Hibernate anyway? It contains a lot of mapping examples, under each undoubtely yours.

http://www.hibernate.org/hib_docs/v3/reference/en/html/index.html

Check chapter 7.
 
Narasimha Swami
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its no way related to JSP. I posted this in wrong category. Well i did read them. As i said it works fine actually, the problem is for a single customer there supposed to be many products related. But when fetched i get only one per customer. I don't know where am i going wrong.
 
Narasimha Swami
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any way i solved this problem by altering the table customer_products as

Table:customer_products
-----------------------
cp_id int PRIMARY KEY
customer_id int
product_id int

Now a new problem arises when saving customer_products i use "Set" as
Customer cust=new Customer();
....set all properties of customer....
CustomerProducts cp=new CustomerProducts();
cp.setCustimerId(cust.getId());
cp.setProduct(1);
cust.getCustomerProducts().add(cp);
cp.setCustimerId(cust.getId());
cp.setProduct(2);
cust.getCustomerProducts().add(cp);

but when i get the size of Set as cust.getCustomerProducts().size(); i get size as 1 i.e. only last record is in Set.
 
Narasimha Swami
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok again i solved my problem by myself by altering overrided hashCode method's definition
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic