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.
|