-->
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.  [ 2 posts ] 
Author Message
 Post subject: Urgent Help - Hibernate Error - High Importance
PostPosted: Sun Nov 30, 2008 8:14 pm 
Newbie

Joined: Sun Nov 30, 2008 7:49 pm
Posts: 1
Hi,

I need an urgent help for the below problem. I am new to Hibernate, I will appreciate if anybody help me on this.

I have two tables:

MySQL Database

TBLORDER
------------
ID - BIGINT (primary key)
ORDERID - INTEGER (primary key)
PRODUCTID - INTEGER (foreign key to TBLPRODUCT PRODUCTID(primary key in this table))

TBLPRODUCT
---------------

ID - BIGINT (primary key)
PRODUCTID - INTEGER (primary key)


In Java Code

Order.java
------------

private long id;
private int orderId;
private Set<Product> products;
... Corresponding get, set and addProduct method for products attribute..

Product.java
---------------
private long id;
private int productId;
....... As usual get and set methods for these attributes...

In mappings xml file

For Product Class
-------------------
<class name="com.onlinestore.model.Product" table="TBLPRODUCT" lazy="true">
<id name="id" type="long" column="ID">
<generator class="native"/>
</id>
<property name="productId" column="PRODUCTID" type="int"/>
.... and remaining properties.....

For Order Class
------------------
<class name="com.onlinestore.model.Order" table="TBLORDER" lazy="true" >
<id name="id" type="long" column="ID">
<generator class="native"/>
</id>
<property name="orderId" column="ORDERID" type="int"/>
<one-to-one name="customer" class="com.onlinestore.model.Customer" property-ref="customerId" cascade="all"/>
<set name="products" table="TBLPRODUCT" cascade="all">
<key column="ID"/>
<many-to-many column="PRODUCTID" class="com.onlinestore.model.Product"/>
</set>
... Other usual mappings...

Note: Please remember that i am not using composite keys, I am just trying to
1. Adding an simple Order
2. Adding two products to Order. Thats why i am using Set for products attribute in Order class.
3. ID, ORDERID is primary key(these are not composite) in TBLORDER Table.
4. ID, PRODUCTID is primary key in TBLPRODUCT table.

[b]I am getting the below problem.[/b]

Initial session factory creation failed : org.hibernate.MappingException: Foreign key (FK50F06771A443D3A7:TBLPRODUCT [PRODUCTID])) must have same number of columns as the referenced primary key (TBLPRODUCT [ID,PRODUCTID])
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.onlinestore.util.HibernateUtil.<clinit>(HibernateUtil.java:22)
at com.onlinestore.dao.OrderDAOImpl.createOrder(OrderDAOImpl.java:25)
at com.onlinestore.handler.OrderHandler.createOrder(OrderHandler.java:74)
at com.onlinestore.handler.OrderHandler.main(OrderHandler.java:83)






[b]Please Help me as i am new to Hibernate and i don't have much time to solve this problem as it is critical. And also plz advice me whether my association is right or not.

I just want to add no. of products to a order.

[/b]

Thank you all.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 01, 2008 4:25 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I have to say that there is not much that seems to match between the tables, mapping files and java code that you show. There are many things that needs to be fixed. I don't know what you need to keep fixed and what is more easily to change.

The first thing you need to fix is to make sure that the primary keys in the tables match what's inside the <id> tags in the mapping files. Currently each of the two tables have two columns that make up the primary key, but in your mapping files you only have a single column in the <id> tag. Personally I would redo the tables and only use a single column as the primary key. If you can't change that you need to go for a composite primary key in the mapping files.

Second thing is that I don't understand how an Order is supposed to reference multiple products. You are using the TBLPRODUCT as a join table in the mapping for Order.getProducts() where you are saying that the PRODUCTID column is the foreign key for the product and the ID column is the foreign key for the order. But in the mapping for Product you are saying that ID is the primary key for Product and that PRODUCTID is just an integer property with no meaning. This doesn't make sense. For a many-to-many relation you are going to need a third table that joins the primary keys for the Order and Product).


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

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.