I have a Table that will not insert correctly. It has some pretty scary composite key relationships. When I try insert a new record it fails with a CampaignId cannot be null error. The mapping files and DAO are generated by Middlegen. I actually looked at the SQL Trace and it doesn't include the Foreign Keys into the Insert Statement.
Any help would be appreciated.
-------------------------------
Mapping File
------------------------------
<?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 2.1
http://boss.bekk.no/boss/middlegen/
http://www.hibernate.org/
-->
<class
name="com.xsoft.estore.dao.Promotion"
table="Promotion"
>
<composite-id name="comp_id" class="com.xsoft.estore.dao.PromotionPK">
<key-property
name="companyCode"
column="CompanyCode"
type="java.lang.String"
length="3"
/>
<key-property
name="storeCode"
column="StoreCode"
type="java.lang.String"
length="3"
/>
<key-property
name="promotionCode"
column="PromotionCode"
type="java.lang.String"
length="12"
/>
</composite-id>
<property
name="description"
type="java.lang.String"
column="Description"
not-null="true"
length="80"
/>
<property
name="longDescription"
type="java.lang.String"
column="LongDescription"
not-null="true"
length="240"
/>
<property
name="launchDate"
type="java.sql.Timestamp"
column="LaunchDate"
not-null="true"
length="23"
/>
<property
name="expirationDate"
type="java.sql.Timestamp"
column="ExpirationDate"
not-null="true"
length="23"
/>
<property
name="useCounter"
type="int"
column="UseCounter"
not-null="true"
length="10"
/>
<property
name="maxUseCounter"
type="int"
column="MaxUseCounter"
not-null="true"
length="10"
/>
<property
name="priorityCode"
type="int"
column="PriorityCode"
not-null="true"
length="10"
/>
<property
name="promotionTypeCode"
type="java.lang.String"
column="PromotionTypeCode"
not-null="true"
length="10"
/>
<property
name="createdBy"
type="java.lang.String"
column="CreatedBy"
not-null="true"
length="30"
/>
<property
name="createdDateTime"
type="java.sql.Timestamp"
column="CreatedDateTime"
not-null="true"
length="23"
/>
<property
name="lastModifiedBy"
type="java.lang.String"
column="LastModifiedBy"
not-null="true"
length="30"
/>
<property
name="lastModifiedDateTime"
type="java.sql.Timestamp"
column="LastModifiedDateTime"
not-null="true"
length="23"
/>
<!-- Associations -->
<!-- derived association(s) for compound key -->
<!-- bi-directional many-to-one association to StoreTbl -->
<many-to-one
name="storeTbl"
class="com.xsoft.estore.dao.StoreTbl"
update="false"
insert="false"
unique="false"
outer-join="auto"
>
<column name="CompanyCode" />
<column name="StoreCode" />
</many-to-one>
<!-- end of derived association(s) -->
<!-- bi-directional many-to-one association to Offer -->
<many-to-one
name="offer"
class="com.xsoft.estore.dao.Offer"
not-null="true"
update="false"
insert="false"
unique="false"
outer-join="auto"
>
<column name="CompanyCode" />
<column name="StoreCode" />
<column name="OfferId" />
</many-to-one>
<!-- bi-directional one-to-many association to PromotionCustomer -->
<set
name="promotionCustomers"
lazy="true"
inverse="true"
cascade="none"
>
<key>
<column name="CompanyCode" />
<column name="StoreCode" />
<column name="PromotionCode" />
</key>
<one-to-many
class="com.xsoft.estore.dao.PromotionCustomer"
/>
</set>
<!-- bi-directional many-to-one association to Campaign -->
<many-to-one
name="campaign"
class="com.xsoft.estore.dao.Campaign"
not-null="true"
update="false"
insert="false"
unique="false"
outer-join="auto"
>
<column name="CompanyCode" />
<column name="StoreCode" />
<column name="CampaignId" />
</many-to-one>
</class>
</hibernate-mapping>
--------------------------------------
Sample Code
-------------------------------------
@Override
public void createPromotion(User currentUser, Promotion promotion)
throws PromotionModelException {
Date currentDate = new Date();
try {
com.xsoft.estore.dao.Promotion promotionTable = map(promotion);
promotionTable.setLastModifiedDateTime(currentDate);
promotionTable.setLastModifiedBy(currentUser.getUserID());
promotionTable.setCreatedDateTime(currentDate);
promotionTable.setCreatedBy(currentUser.getUserID());
DAOSession daoSession = DAOeStoreBackend.getSession();
try {
daoSession.beginTransaction();
daoSession.getSession().save(promotionTable);
daoSession.commitTransaction();
} catch (Exception exc) {
daoSession.rollbackTransaction();
throw new DAOException(
"DAOPromotionDAO.createPromotion() insert failed!", exc);
} finally {
DAOeStoreBackend.closeSession();
}
} catch (Exception exc) {
throw new PromotionModelException(
"DAOPromotionDAO.createPromotion() failed.", exc);
}
}
protected com.xsoft.estore.dao.Promotion map(Promotion promotion)
throws PromotionModelException {
try {
DAOSession daoSession = DAOeStoreBackend.getSession();
com.xsoft.estore.dao.Promotion promotionTbl = new com.xsoft.estore.dao.Promotion();
// Promotion Primary Key
promotionTbl.setComp_id(new com.xsoft.estore.dao.PromotionPK());
promotionTbl.getComp_id()
.setCompanyCode(promotion.getCompanyCode());
promotionTbl.getComp_id().setStoreCode(promotion.getStoreCode());
promotionTbl.getComp_id().setPromotionCode(
promotion.getPromotionCode());
promotionTbl.setDescription(promotion.getDescription());
promotionTbl.setLongDescription(promotion.getLongDescription());
promotionTbl.setLaunchDate(promotion.getLaunchDate());
promotionTbl.setExpirationDate(promotion.getEndDate());
// Campaign Foreign Key
promotionTbl.setCampaign((Campaign) daoSession.getSession().get(
Campaign.class,
new CampaignPK(promotion.getCompanyCode(), promotion
.getStoreCode(), promotion.getCampaignCode())));
// Offer Foreign Key
promotionTbl.setOffer((Offer) daoSession.getSession().get(
Offer.class,
new OfferPK(promotion.getCompanyCode(), promotion
.getStoreCode(), promotion.getOfferId())));
promotionTbl.setUseCounter(promotion.getUseCounter());
promotionTbl.setMaxUseCounter(promotion.getMaxUseCounter());
promotionTbl.setPriorityCode(promotion.getPriority());
promotionTbl.setPromotionTypeCode(promotion.getPromoTypeCode());
promotionTbl.setLastModifiedDateTime(Util.getDate(promotion
.getModifiedDate()));
promotionTbl.setLastModifiedBy(promotion.getModifiedUserID());
promotionTbl.setCreatedDateTime(Util.getDate(promotion
.getCreatedDate()));
promotionTbl.setCreatedBy(promotion.getCreatedUserID());
return promotionTbl;
} catch (Exception exc) {
throw new PromotionModelException("DAOPromotionDAO.map() failed.",
exc);
}
}