Ajuda para salvar em duas tabelas

Viva Pessoal,

Eu tenho um ficheiro hbm que tem o seguinte codigo entre outro:

<joined-subclass                       
            name="com.retek.rpm.domain.promotion.bo.TslPromotionComponentMultiBuyDetailImpl"
            table="TSL_RPM_PROMO_COMP_M_B" dynamic-update="true"
            persister="com.retek.rpm.domain.core.dao.RetekNormalizedEntityPersister">
              <meta attribute="lazy-load-collection">zoneGroupNodes</meta>
         <key column="RPM_PROMO_COMP_DETAIL_ID"/>
                                       
            <property name="multiBuyPromotionType" column="MULTI_BUY_PROMO_TYPE" type="com.retek.rpm.domain.core.dao.codeconverter.TslMultiBuyTypeStringCodeConverter" not-null="true"/>
            <!-- coupon to be included -->

            <set name="listDetails" inverse="true" lazy="true">
            <key column="RPM_PROMO_COMP_DETAIL_ID"/>
            <one-to-many class="com.retek.rpm.domain.promotion.bo.TslMultiBuyListImpl"/>
         </set>
</joined-subclass>

A classe TslMultiBuyListImpl é composta pelo seguinte:

public class TslMultiBuyListImpl extends FSMAbleBusinessObject implements TslMultiBuyList  {

	private String listDisplayId;

	private TslMultiBuyListTypeCode listTypes;

	private TslBuyItemTypeCode buyListType;

	private Integer value;

	private TslMultiBuyPromotionChangeTypeCode promotionChangeTypeCode;

	private Quantity getQuantity;

	private ListableVO unitOfMeasureCode;
	
	private RetailChangeValue retailChangeValue;
	
	private Set buyItems = new HashSet();
	private Set getItems = new HashSet();
	
	private int lockVersion;

	private Integer id;
	
	public TslMultiBuyListImpl() throws RetekBusinessException {
		super();
	}
	
	public TslMultiBuyListImpl(ObjectId id) throws RetekBusinessException {
		super(id);
	}	

	public TslBuyItemTypeCode getBuyListType() {
		return buyListType;
	}

	public void setBuyListType(TslBuyItemTypeCode buyListType) {
		this.buyListType = buyListType;
	}

	public Quantity getGetQuantity() {
		return getQuantity;
	}

	public void setGetQuantity(Quantity getQuantity) {
		this.getQuantity = getQuantity;
	}

	public String getListDisplayId() {
		return listDisplayId;
	}

	public void setListDisplayId(String listDisplayId) {
		this.listDisplayId = listDisplayId;
	}

	public TslMultiBuyListTypeCode getListTypes() {
		return listTypes;
	}

	public void setListTypes(TslMultiBuyListTypeCode listTypes) {
		this.listTypes = listTypes;
	}

	public TslMultiBuyPromotionChangeTypeCode getPromotionChangeTypeCode() {
		return promotionChangeTypeCode;
	}

	public void setPromotionChangeTypeCode(
			TslMultiBuyPromotionChangeTypeCode promotionChangeTypeCode) {
		this.promotionChangeTypeCode = promotionChangeTypeCode;
	}

	public ListableVO getUnitOfMeasureCode() {
		return unitOfMeasureCode;
	}

	public void setUnitOfMeasureCode(ListableVO unitOfMeasureCode) {
		this.unitOfMeasureCode = unitOfMeasureCode;
	}

	public Integer getValue() {
		return value;
	}

	public void setValue(Integer value) {
		this.value = value;
	}
	
	public RetailChangeValue getRetailChangeValue() {
		return retailChangeValue;
	}

	public void setRetailChangeValue(RetailChangeValue retailChangeValue) {
		this.retailChangeValue = retailChangeValue;
	}


    public void addAllGetItems(Set /* <Item> */getItems) {
        this.getItems.addAll(getItems);
    }

    public void addAllBuyItems(Set /* <Item> */buyItems) {
        this.buyItems.addAll(buyItems);
    }
    
    public Set getBuyItems() {
		return buyItems;
	}
    
    public Set getGetItems() {
		return getItems;
	}


	public int getLockVersion() {
		return lockVersion;
	}

	public void setLockVersion(int lockVersion) {
		this.lockVersion = lockVersion;
	}
	
	public Integer getId(){
		return id;
	}

	public Class getStateModelClass() {
		return null;
	}
	
		
}

Também criei um ficheiro hbm para esta classe:

<?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>
	    <class name="com.retek.rpm.domain.promotion.bo.TslMultiBuyListImpl" table="TSL_RPM_PROMO_COMP_M_B_DTL">			
				<composite-id name="objectId" class="com.retek.platform.bo.LongObjectId" access="field" 
				    unsaved-value="any">
				    <key-property name="value" column="LIST_ID" type="long" 
				        access="field"/>
				</composite-id>
		   	 	<property name="listDisplayId" column="LIST_ID" insert="false" update="false" access="field"/>                              	
		        <property name="listTypes" column="LIST_TYPE"  type="com.retek.rpm.domain.core.dao.codeconverter.TslMultiBuyListTypeCodeConverter" insert="false" update="false" access="field"/>
				<property name="buyListType" column="BUY_ITEM_TYPE" type="com.retek.rpm.domain.core.dao.codeconverter.TslMultiBuyPromotionBuyTypeCodeConverter" insert="false" update="false" access="field"/>
				<property name="value" column="BUY_ITEM_VALUE" insert="false" update="false" access="field"/>
				<property name="retailChangeValue" access="field" 
					type="com.retek.rpm.domain.promotion.dao.RetailChangeValueUserType" insert="false" update="false">
					<column name="CHANGE_TYPE" not-null="false"/>
					<column name="CHANGE_AMOUNT" not-null="false"/>
					<column name="CHANGE_CURRENCY" not-null="false"/>
					<column name="CHANGE_PERCENT" not-null="false"/>
					<column name="CHANGE_SELLING_UOM" not-null="false"/>
					<column name="VOUCHER_NUMBER" not-null="false"/>
					<column name="VOUCHER_DESCRIPTION" not-null="false"/>
		        </property>
		      	<property name="lockVersion" column="LOCK_VERSION" not-null="false" insert="false" update="false" access="field"/>
		      	<property name="getQuantity" column="GET_QUANTITY" type="com.retek.rpm.domain.promotion.dao.QuantityUserType" not-null="false" insert="false" update="false" access="field"/>
			</class>			
</hibernate-mapping>

O problema que estou a ter é o seguinte:

  • É inserido um registo na tabela: TSL_RPM_PROMO_COMP_M_B
  • Nenhum registo é inserido na tabela referênciada pelo hbm da classe TslMultiBuyListImpl.
  • Nenhum excepção é gerada.

Alguem tem resposta para o que me está a acontecer?

Muito Obrigado
Texugo

Viva Pessoal,

Em tutoriais que andei a ler vi que teria que colocar no elemento SET do primeiro ficheiro hbm a propriedade cascade=“save-update”. Apos ter colocado esta tag é gerada um excepção que mostro em baixo.

[b]
Caused by: net.sf.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): com.retek.rpm.domain.promotion.bo.TslMultiBuyListImpl

  at net.sf.hibernate.id.Assigned.generate(Assigned.java:26)

  at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:776)

  at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:749)

  at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1398)

  at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:114)

  at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436)

  at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:526)

  at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:452)

  at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503)

  at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:962)

  at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:868)

  at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:790)

  at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:749)

  at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1398)

  at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:114)

  at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436)

  at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:526)

  at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:452)

  at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503)

  at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:962)

  at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:868)

  at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:790)

  at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:749)

  at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1398)

  at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:114)

  at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436)

  at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:526)

  at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:452)

  at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503)

  at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:482)

  at net.sf.hibernate.impl.SessionImpl.preFlushEntities(SessionImpl.java:2701)

  at net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:2271)

  at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2260)

  at com.retek.platform.persistence.hibernate.HibernateSessionDispenser.flushCurrent(HibernateSessionDispenser.java:404)

  at com.retek.platform.service.HibernateJ2EEServiceContext.checkpoint(HibernateJ2EEServiceContext.java:116)

  at com.retek.rpm.domain.core.persistence.PersistenceManagerImpl.flush(PersistenceManagerImpl.java:194)

  at com.retek.rpm.domain.promotion.fsm.SaveComponentDetailsAction.justDoIt(SaveComponentDetailsAction.java:39)

[/b]

Como é que posso colocar manualmente o ID que me é pedido?

Alguem já teve este problema?

Cumps
Rui Félix

Viva pessoal,

Já resolvi o prob. Tive que acrescentar a tag Generator para o segundo ficheiro hbm.xml.

Cumps
Rui Felix