Hibernate version:3.1.3
Spring version:2.0.RC2
Oracle version:10g
JDK 5
I am new to Hibernate and trying to do the right things. Here is my scenario. I have a customer and order objects and they are being persisted to database tables with same names.
1- An external system generates order that is insert a new order in the table by assigning the id (this is unfilled order) and email to customer a link that customer can follow to complete the order form. The email address of the customer is captured in the ONLINE_ORDER table.
2- Customer will click on the link and fill the order form and submit it. After submission application will validates and if every thing is valid, then it will do insert for customer information on the ORDER_CUSTOMER table if customer information is not already in the table if it finds the customer information is there then it should just get the id (This id needs to be persisted on ONLINE_ORDER table).
Code:
Customer {
id: long
name: String
email: String
phone: String
……
}
Order {
id: long
customer: Customer
orderText: String
submissionDate: Date
orderCode: String
. ….
}
Mapping documents:Code:
<hibernate-mapping package="com.myCompany.enterprise.order.domain" default-lazy="false">
<class name="Order" table="ONLINE_ORDER" >
<id name="id" column=" ONLINE_ORDER _ID" type="long" />
<property name=" orderText" column="ORDER_TEXT" />
<property name="submissionDate" column="SUBMISSION_DATE" type="date" />
<property name=" orderCode " column="ORDER_TYPE_CD" update="false" />
<many-to-one name="customer" column=" CUSTOMER _ID" class=" Customer " cascade="save-update"/>
</class>
<class name="Customer" table="ORDER_CUSTOMER">
<id name="id" column="CUSTOMER_ID" unsaved-value="0">
<generator class="sequence">
<param name="sequence">CUSTOMER_ID_SEQ</param>
</generator>
</id>
<property name="name" />
<property name="email" column="EMAIL_ADDR" />
<property name="phone" column="CONTACT_PHONE_NO" />
</class>
</hibernate-mapping>
I call my OrderDao.save( ) method to save the order and customer (if needed).
My problem:
1- I have a method isCustomerExist( ) that I call to check if customer exist in the database just before I call OrderDao.save method. If customer exist then it just sets passed customer id and return true otherwise it just return false.
Is there a way with hibernate to do this kind of thing.
2- OrderDao.save ( ) method save the order. I am using cascade “save-update” and if customer does not exist it correctly does the insert but if customer does exist then it does update that is un-necessary. How can I eliminate that. I tried cascade = “merge” and “persist” but both gives TransientObjectException when adding new customer.
Help will be greatly appreciated.