Hi All,
I am new to hibernate. I tried to persist Sales Order object (one to many) but it is saving the data only in the master table not in the detail table. The mapping files were generated by Middlegen plugin and POJO classes by hbm2hava tool. The data is being populated through Swing form.
Here are the mappping files-
=====SamOrderM.hbm.xml=====
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin
http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->
<class
name="com.agileObjects.sam.hibernate.SamOrderM"
table="SAM_ORDER_M"
>
<meta attribute="class-description" inherit="false">
Sales Order Master class
</meta>
<id
name="orderOrderNum"
type="java.lang.String"
column="ORDER_ORDER_NUM"
>
<generator class="assigned" />
</id>
<property
name="orderDebtorCode"
type="java.lang.String"
column="ORDER_DEBTOR_CODE"
length="7"
>
<meta attribute="field-description">
Customer code
</meta>
</property>
<property
name="orderOrderDate"
type="java.sql.Date"
column="ORDER_ORDER_DATE"
length="10"
>
<meta attribute="field-description">
Sales order date
</meta>
</property>
<property
name="orderPoNum"
type="java.lang.String"
column="ORDER_PO_NUM"
length="25"
>
<meta attribute="field-description">
Customer's PO number
</meta>
</property>
<property
name="orderPoDate"
type="java.sql.Date"
column="ORDER_PO_DATE"
length="10"
>
<meta attribute="field-description">
Customer's PO Date
</meta>
</property>
<property
name="orderAdvanceFlag"
type="java.lang.String"
column="ORDER_ADVANCE_FLAG"
length="1"
/>
<property
name="orderAdvanceAmount"
type="java.math.BigDecimal"
column="ORDER_ADVANCE_AMOUNT"
length="10"
/>
<property
name="orderSpecificInst"
type="java.lang.String"
column="ORDER_SPECIFIC_INST"
length="60"
/>
<property
name="orderFormCode"
type="java.lang.Integer"
column="ORDER_FORM_CODE"
length="3"
/>
<property
name="orderDiscountPercent"
type="java.math.BigDecimal"
column="ORDER_DISCOUNT_PERCENT"
length="5"
/>
<property
name="orderHandlingPercent"
type="java.math.BigDecimal"
column="ORDER_HANDLING_PERCENT"
length="5"
/>
<property
name="orderFreightPercent"
type="java.math.BigDecimal"
column="ORDER_FREIGHT_PERCENT"
length="5"
/>
<property
name="orderCstlstPercent"
type="java.math.BigDecimal"
column="ORDER_CSTLST_PERCENT"
length="5"
/>
<property
name="orderInitialValue"
type="java.math.BigDecimal"
column="ORDER_INITIAL_VALUE"
length="12"
/>
<property
name="orderActualValue"
type="java.math.BigDecimal"
column="ORDER_ACTUAL_VALUE"
length="12"
/>
<property
name="orderStatusFlag"
type="java.lang.String"
column="ORDER_STATUS_FLAG"
length="1"
/>
<property
name="orderPaymentThru"
type="java.lang.String"
column="ORDER_PAYMENT_THRU"
length="1"
/>
<property
name="orderBankCode"
type="java.lang.String"
column="ORDER_BANK_CODE"
length="6"
/>
<!-- associations -->
<!-- bi-directional one-to-many association to SamOrderD -->
<set
name="samOrderDs"
lazy="true"
inverse="true"
>
<key>
<column name="ORDER_ORDER_NUM" />
</key>
<one-to-many
class="com.agileObjects.sam.hibernate.SamOrderD"
/>
</set>
</class>
</hibernate-mapping>
=====SamOrderD.hbm.xml=====
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin
http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->
<class
name="com.agileObjects.sam.hibernate.SamOrderD"
table="SAM_ORDER_D"
>
<meta attribute="class-description" inherit="false">
Sales Order Detail (lines) class
</meta>
<composite-id name="comp_id" class="com.agileObjects.sam.hibernate.SamOrderDPK">
<key-property
name="orderItemCode"
column="ORDER_ITEM_CODE"
type="java.lang.String"
length="25"
/>
<!-- bi-directional many-to-one association to SamOrderM -->
<key-many-to-one
name="samOrderM"
class="com.agileObjects.sam.hibernate.SamOrderM"
>
<column name="ORDER_ORDER_NUM" />
</key-many-to-one>
</composite-id>
<property
name="orderItemQuantity"
type="java.math.BigDecimal"
column="ORDER_ITEM_QUANTITY"
length="10"
>
<meta attribute="field-description">
order quantity
</meta>
</property>
<property
name="itemUnitRate"
type="java.math.BigDecimal"
column="ITEM_UNIT_RATE"
length="10"
/>
<property
name="itemSpecialDiscount"
type="java.math.BigDecimal"
column="ITEM_SPECIAL_DISCOUNT"
length="5"
/>
<property
name="orderItemValue"
type="java.math.BigDecimal"
column="ORDER_ITEM_VALUE"
length="10"
/>
<property
name="orderQuantityDisp"
type="java.math.BigDecimal"
column="ORDER_QUANTITY_DISP"
length="10"
/>
<property
name="orderQuantityPending"
type="java.math.BigDecimal"
column="ORDER_QUANTITY_PENDING"
length="10"
/>
<property
name="orderPendingDa"
type="java.math.BigDecimal"
column="ORDER_PENDING_DA"
length="10"
/>
<property
name="orderOpenFlag"
type="java.lang.String"
column="ORDER_OPEN_FLAG"
length="1"
/>
<!-- associations -->
</class>
</hibernate-mapping>
------------------------------------------------------------
Here are the POJO files-
=====SamOrderM.java=====
package com.agileObjects.sam.hibernate;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Set;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
/**
* Sales Order Master class
*
*/
public class SamOrderM implements Serializable {
/** identifier field */
private String orderOrderNum;
/** nullable persistent field */
private String orderDebtorCode;
/** nullable persistent field */
private Date orderOrderDate;
/** nullable persistent field */
private String orderPoNum;
/** nullable persistent field */
private Date orderPoDate;
/** nullable persistent field */
private String orderAdvanceFlag;
/** nullable persistent field */
private BigDecimal orderAdvanceAmount;
/** nullable persistent field */
private String orderSpecificInst;
/** nullable persistent field */
private Integer orderFormCode;
/** nullable persistent field */
private BigDecimal orderDiscountPercent;
/** nullable persistent field */
private BigDecimal orderHandlingPercent;
/** nullable persistent field */
private BigDecimal orderFreightPercent;
/** nullable persistent field */
private BigDecimal orderCstlstPercent;
/** nullable persistent field */
private BigDecimal orderInitialValue;
/** nullable persistent field */
private BigDecimal orderActualValue;
/** nullable persistent field */
private String orderStatusFlag;
/** nullable persistent field */
private String orderPaymentThru;
/** nullable persistent field */
private String orderBankCode;
/** persistent field */
private Set samOrderDs;
/** full constructor */
public SamOrderM(String orderOrderNum, String orderDebtorCode, Date orderOrderDate, String orderPoNum, Date orderPoDate, String orderAdvanceFlag, BigDecimal orderAdvanceAmount, String orderSpecificInst, Integer orderFormCode, BigDecimal orderDiscountPercent, BigDecimal orderHandlingPercent, BigDecimal orderFreightPercent, BigDecimal orderCstlstPercent, BigDecimal orderInitialValue, BigDecimal orderActualValue, String orderStatusFlag, String orderPaymentThru, String orderBankCode, Set samOrderDs) {
this.orderOrderNum = orderOrderNum;
this.orderDebtorCode = orderDebtorCode;
this.orderOrderDate = orderOrderDate;
this.orderPoNum = orderPoNum;
this.orderPoDate = orderPoDate;
this.orderAdvanceFlag = orderAdvanceFlag;
this.orderAdvanceAmount = orderAdvanceAmount;
this.orderSpecificInst = orderSpecificInst;
this.orderFormCode = orderFormCode;
this.orderDiscountPercent = orderDiscountPercent;
this.orderHandlingPercent = orderHandlingPercent;
this.orderFreightPercent = orderFreightPercent;
this.orderCstlstPercent = orderCstlstPercent;
this.orderInitialValue = orderInitialValue;
this.orderActualValue = orderActualValue;
this.orderStatusFlag = orderStatusFlag;
this.orderPaymentThru = orderPaymentThru;
this.orderBankCode = orderBankCode;
this.samOrderDs = samOrderDs;
}
/** default constructor */
public SamOrderM() {
}
/** minimal constructor */
public SamOrderM(String orderOrderNum, Set samOrderDs) {
this.orderOrderNum = orderOrderNum;
this.samOrderDs = samOrderDs;
}
public String getOrderOrderNum() {
return this.orderOrderNum;
}
public void setOrderOrderNum(String orderOrderNum) {
this.orderOrderNum = orderOrderNum;
}
/**
* Customer code
*
*/
public String getOrderDebtorCode() {
return this.orderDebtorCode;
}
public void setOrderDebtorCode(String orderDebtorCode) {
this.orderDebtorCode = orderDebtorCode;
}
/**
* Sales order date
*
*/
public Date getOrderOrderDate() {
return this.orderOrderDate;
}
public void setOrderOrderDate(Date orderOrderDate) {
this.orderOrderDate = orderOrderDate;
}
/**
* Customer's PO number
*
*/
public String getOrderPoNum() {
return this.orderPoNum;
}
public void setOrderPoNum(String orderPoNum) {
this.orderPoNum = orderPoNum;
}
/**
* Customer's PO Date
*
*/
public Date getOrderPoDate() {
return this.orderPoDate;
}
public void setOrderPoDate(Date orderPoDate) {
this.orderPoDate = orderPoDate;
}
public String getOrderAdvanceFlag() {
return this.orderAdvanceFlag;
}
public void setOrderAdvanceFlag(String orderAdvanceFlag) {
this.orderAdvanceFlag = orderAdvanceFlag;
}
public BigDecimal getOrderAdvanceAmount() {
return this.orderAdvanceAmount;
}
public void setOrderAdvanceAmount(BigDecimal orderAdvanceAmount) {
this.orderAdvanceAmount = orderAdvanceAmount;
}
public String getOrderSpecificInst() {
return this.orderSpecificInst;
}
public void setOrderSpecificInst(String orderSpecificInst) {
this.orderSpecificInst = orderSpecificInst;
}
public Integer getOrderFormCode() {
return this.orderFormCode;
}
public void setOrderFormCode(Integer orderFormCode) {
this.orderFormCode = orderFormCode;
}
public BigDecimal getOrderDiscountPercent() {
return this.orderDiscountPercent;
}
public void setOrderDiscountPercent(BigDecimal orderDiscountPercent) {
this.orderDiscountPercent = orderDiscountPercent;
}
public BigDecimal getOrderHandlingPercent() {
return this.orderHandlingPercent;
}
public void setOrderHandlingPercent(BigDecimal orderHandlingPercent) {
this.orderHandlingPercent = orderHandlingPercent;
}
public BigDecimal getOrderFreightPercent() {
return this.orderFreightPercent;
}
public void setOrderFreightPercent(BigDecimal orderFreightPercent) {
this.orderFreightPercent = orderFreightPercent;
}
public BigDecimal getOrderCstlstPercent() {
return this.orderCstlstPercent;
}
public void setOrderCstlstPercent(BigDecimal orderCstlstPercent) {
this.orderCstlstPercent = orderCstlstPercent;
}
public BigDecimal getOrderInitialValue() {
return this.orderInitialValue;
}
public void setOrderInitialValue(BigDecimal orderInitialValue) {
this.orderInitialValue = orderInitialValue;
}
public BigDecimal getOrderActualValue() {
return this.orderActualValue;
}
public void setOrderActualValue(BigDecimal orderActualValue) {
this.orderActualValue = orderActualValue;
}
public String getOrderStatusFlag() {
return this.orderStatusFlag;
}
public void setOrderStatusFlag(String orderStatusFlag) {
this.orderStatusFlag = orderStatusFlag;
}
public String getOrderPaymentThru() {
return this.orderPaymentThru;
}
public void setOrderPaymentThru(String orderPaymentThru) {
this.orderPaymentThru = orderPaymentThru;
}
public String getOrderBankCode() {
return this.orderBankCode;
}
public void setOrderBankCode(String orderBankCode) {
this.orderBankCode = orderBankCode;
}
public Set getSamOrderDs() {
return this.samOrderDs;
}
public void setSamOrderDs(Set samOrderDs) {
this.samOrderDs = samOrderDs;
}
public String toString() {
return new ToStringBuilder(this)
.append("orderOrderNum", getOrderOrderNum())
.toString();
}
public boolean equals(Object other) {
if ( !(other instanceof SamOrderM) ) return false;
SamOrderM castOther = (SamOrderM) other;
return new EqualsBuilder()
.append(this.getOrderOrderNum(), castOther.getOrderOrderNum())
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder()
.append(getOrderOrderNum())
.toHashCode();
}
}
=====SamOrderD.java=====
package com.agileObjects.sam.hibernate;
import java.io.Serializable;
import java.math.BigDecimal;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
/**
* Sales Order Detail (lines) class
*
*/
public class SamOrderD implements Serializable {
/** identifier field */
private com.agileObjects.sam.hibernate.SamOrderDPK comp_id;
/** nullable persistent field */
private BigDecimal orderItemQuantity;
/** nullable persistent field */
private BigDecimal itemUnitRate;
/** nullable persistent field */
private BigDecimal itemSpecialDiscount;
/** nullable persistent field */
private BigDecimal orderItemValue;
/** nullable persistent field */
private BigDecimal orderQuantityDisp;
/** nullable persistent field */
private BigDecimal orderQuantityPending;
/** nullable persistent field */
private BigDecimal orderPendingDa;
/** nullable persistent field */
private String orderOpenFlag;
/** full constructor */
public SamOrderD(com.agileObjects.sam.hibernate.SamOrderDPK comp_id, BigDecimal orderItemQuantity, BigDecimal itemUnitRate, BigDecimal itemSpecialDiscount, BigDecimal orderItemValue, BigDecimal orderQuantityDisp, BigDecimal orderQuantityPending, BigDecimal orderPendingDa, String orderOpenFlag) {
this.comp_id = comp_id;
this.orderItemQuantity = orderItemQuantity;
this.itemUnitRate = itemUnitRate;
this.itemSpecialDiscount = itemSpecialDiscount;
this.orderItemValue = orderItemValue;
this.orderQuantityDisp = orderQuantityDisp;
this.orderQuantityPending = orderQuantityPending;
this.orderPendingDa = orderPendingDa;
this.orderOpenFlag = orderOpenFlag;
}
/** default constructor */
public SamOrderD() {
}
/** minimal constructor */
public SamOrderD(com.agileObjects.sam.hibernate.SamOrderDPK comp_id) {
this.comp_id = comp_id;
}
public com.agileObjects.sam.hibernate.SamOrderDPK getComp_id() {
return this.comp_id;
}
public void setComp_id(com.agileObjects.sam.hibernate.SamOrderDPK comp_id) {
this.comp_id = comp_id;
}
/**
* order quantity
*
*/
public BigDecimal getOrderItemQuantity() {
return this.orderItemQuantity;
}
public void setOrderItemQuantity(BigDecimal orderItemQuantity) {
this.orderItemQuantity = orderItemQuantity;
}
public BigDecimal getItemUnitRate() {
return this.itemUnitRate;
}
public void setItemUnitRate(BigDecimal itemUnitRate) {
this.itemUnitRate = itemUnitRate;
}
public BigDecimal getItemSpecialDiscount() {
return this.itemSpecialDiscount;
}
public void setItemSpecialDiscount(BigDecimal itemSpecialDiscount) {
this.itemSpecialDiscount = itemSpecialDiscount;
}
public BigDecimal getOrderItemValue() {
return this.orderItemValue;
}
public void setOrderItemValue(BigDecimal orderItemValue) {
this.orderItemValue = orderItemValue;
}
public BigDecimal getOrderQuantityDisp() {
return this.orderQuantityDisp;
}
public void setOrderQuantityDisp(BigDecimal orderQuantityDisp) {
this.orderQuantityDisp = orderQuantityDisp;
}
public BigDecimal getOrderQuantityPending() {
return this.orderQuantityPending;
}
public void setOrderQuantityPending(BigDecimal orderQuantityPending) {
this.orderQuantityPending = orderQuantityPending;
}
public BigDecimal getOrderPendingDa() {
return this.orderPendingDa;
}
public void setOrderPendingDa(BigDecimal orderPendingDa) {
this.orderPendingDa = orderPendingDa;
}
public String getOrderOpenFlag() {
return this.orderOpenFlag;
}
public void setOrderOpenFlag(String orderOpenFlag) {
this.orderOpenFlag = orderOpenFlag;
}
public String toString() {
return new ToStringBuilder(this)
.append("comp_id", getComp_id())
.toString();
}
public boolean equals(Object other) {
if ( !(other instanceof SamOrderD) ) return false;
SamOrderD castOther = (SamOrderD) other;
return new EqualsBuilder()
.append(this.getComp_id(), castOther.getComp_id())
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder()
.append(getComp_id())
.toHashCode();
}
}
=====SamOrderDPK.java=====
package com.agileObjects.sam.hibernate;
import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
/** @author Hibernate CodeGenerator */
public class SamOrderDPK implements Serializable {
/** identifier field */
private String orderItemCode;
/** identifier field */
private com.agileObjects.sam.hibernate.SamOrderM samOrderM;
/** full constructor */
public SamOrderDPK(String orderItemCode, com.agileObjects.sam.hibernate.SamOrderM samOrderM) {
this.orderItemCode = orderItemCode;
this.samOrderM = samOrderM;
}
/** default constructor */
public SamOrderDPK() {
}
public String getOrderItemCode() {
return this.orderItemCode;
}
public void setOrderItemCode(String orderItemCode) {
this.orderItemCode = orderItemCode;
}
public com.agileObjects.sam.hibernate.SamOrderM getSamOrderM() {
return this.samOrderM;
}
public void setSamOrderM(com.agileObjects.sam.hibernate.SamOrderM samOrderM) {
this.samOrderM = samOrderM;
}
public String toString() {
return new ToStringBuilder(this)
.append("orderItemCode", getOrderItemCode())
.append("samOrderM", getSamOrderM())
.toString();
}
public boolean equals(Object other) {
if ( !(other instanceof SamOrderDPK) ) return false;
SamOrderDPK castOther = (SamOrderDPK) other;
return new EqualsBuilder()
.append(this.getOrderItemCode(), castOther.getOrderItemCode())
.append(this.getSamOrderM(), castOther.getSamOrderM())
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder()
.append(getOrderItemCode())
.append(getSamOrderM())
.toHashCode();
}
}
--------------------------------------------------------------------------
To persist the object I am doing as described below-
In constructor of the Swing form
Configuration cfg = new Configuration();
cfg.addClass(SamOrderM.class);
cfg.addClass(SamOrderD.class);
factory = cfg.buildSessionFactory();
In save method
SamOrderM m = new SamOrderM();
//set m properties
Set orderItems = new HashSet();
SamOrderD d = new SamOrderD();
SamOrderDPK dPk = new SamOrderDPK(..., m);
//set d properties
orderItems.add(d);
m.setSamOrderDs(orderItems);
Session session = factory.openSession(DbConnection.con);
session.save(m);
session.flush();
DbConnection.con.commit();
session.close();
------------------------------------------------------------------------
This whole exercise persists only in the master table nothing goes to detail table. I am using Hibernate 2.1 with SAPDB. No hibernate properties or configuration files were used during runtime.
Any thoughts where I am wrong.
Best Regards,