-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Hibernate returns Unique Constraint violated
PostPosted: Sun Oct 28, 2007 1:39 am 
Newbie

Joined: Sun Oct 28, 2007 1:21 am
Posts: 5
Hi to all,

I am new using hibernate and I really need you help guys. I'm having some problems with the error that I'm getting in Hibernat, that is "Unique Constraint Violated". I know what the error means but I don't know how to solve it with Hibernate.

The cenario is simple:

I have two tables, with the relation 1 to many. In the second table my primary key is of the first table and the primary key of the second table.

for this I have a class that has the attribute for the first table and a Set that contains the attributes for the second table.

The problem here is that hibernate is inserting two times the same record in the database that's why it gives me the exception.

With debuging I'm really sure that I don't have duplicated values inside the set so it must be something that I'm doing wrong on the hbm.xml file.

Bellow you'll find the hbm.xml file:

Code:
        <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" table="TSL_RPM_PROMO_COMP_M_B_DTL"
                persister="com.retek.rpm.domain.core.dao.RetekBasicCollectionPersister">
            <key column="RPM_PROMO_COMP_DETAIL_ID"/>                                               
                <composite-element class="com.retek.rpm.domain.promotion.bo.TslMultiBuyListImpl">   
                    <property name="listDisplayId" column="LIST_ID"/>                                 
                   <property name="listTypes" column="LIST_TYPE"  type="com.retek.rpm.domain.core.dao.codeconverter.TslMultiBuyListTypeCodeConverter"/>
               <property name="buyListType" column="BUY_ITEM_TYPE" type="com.retek.rpm.domain.core.dao.codeconverter.TslMultiBuyPromotionBuyTypeCodeConverter"/>
                   <property name="value" column="BUY_ITEM_VALUE"/>
                   <property name="retailChangeValue" access="field"
                      type="com.retek.rpm.domain.promotion.dao.RetailChangeValueUserType">
                        <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"/>
              <property name="getQuantity" column="GET_QUANTITY" type="com.retek.rpm.domain.promotion.dao.QuantityUserType" not-null="false"/>                                  
                </composite-element>


                </set>
         </joined-subclass>




Bellow you'll find the exception:

07/10/28 10:59:44 Hibernate: insert into TSL_RPM_PROMO_COMP_M_B_DTL (RPM_PROMO_COMP_DETAIL_ID, LIST_ID, LIST_TYPE, BUY_I

TEM_TYPE, BUY_ITEM_VALUE, CHANGE_TYPE, CHANGE_AMOUNT, CHANGE_CURRENCY, CHANGE_PERCENT, CHANGE_SELLING_UOM, VOUCHER_NUMBE

R, VOUCHER_DESCRIPTION, LOCK_VERSION, GET_QUANTITY) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

10:59:44,732 ERROR (RMICallHandler-0) [JDBCExceptionReporter] ORA-00001: unique constraint (RMS12.PKEY) violated



10:59:44,732 ERROR (RMICallHandler-0) [JDBCExceptionReporter] ORA-00001: unique constraint (RMS12.PKEY) violated



10:59:44,732 ERROR (RMICallHandler-0) [JDBCExceptionReporter] ORA-00001: unique constraint (RMS12.PKEY) violated



10:59:44,732 ERROR (RMICallHandler-0) [JDBCExceptionReporter] ORA-00001: unique constraint (RMS12.PKEY) violated



10:59:44,732 ERROR (RMICallHandler-0) [SessionImpl] Could not synchronize database state with session

2007-10-28 10:59:44.732 ERROR J2EE EJB8006 [PromotionComponentAppService:public com.retek.rpm.app.promotion.vo.Promotion

ComponentVO com.retek.rpm.app.promotion.service.PromotionComponentAppServiceEjb.save(com.retek.platform.service.ClientCo

ntext,java.lang.String,com.retek.platform.bo.IdentifiableReference,com.retek.rpm.app.promotion.event.PromotionComponentM

odifyEvent,java.util.List,java.util.List,java.lang.Boolean) throws com.retek.platform.exception.RetekBusinessException]

exception occurred during method invocation: oracle.oc4j.rmi.OracleRemoteException: com.retek.platform.persistence.Persi

stenceException: ERROR while flushing a Hibernate Session in HibernateJ2EEServiceContext.onComplete()

exception id: 1193549384732; nested exception is:

com.retek.platform.persistence.PersistenceException: ERROR while flushing a Hibernate Session in HibernateJ2EESe

rviceContext.onComplete()

exception id: 1193549384732




The question is, What am I doing wrong????

Many Thanks
Rui Felix


Top
 Profile  
 
 Post subject: Hibernate returns Unique Constraint violated
PostPosted: Sun Oct 28, 2007 8:48 am 
Newbie

Joined: Sun Oct 28, 2007 1:21 am
Posts: 5
Greetings,

I have changed the hbm file from what I had before for the following:

Code:
<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>


and for TslMultiBuyListImpl I've created a hbm also tha is:

Code:
<?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>



The Class TslMultiBuyListImpl is:

Code:

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;
   }
   
      
}




For this no exception is thown, a record is inserted in the table "TSL_RPM_PROMO_COMP_M_B" but no record is inserted on the table inside the set "TSL_RPM_PROMO_COMP_M_B_DTL".

Does any one had this problem before?
Can someone help me?

Many Thanks
Texugo


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 28, 2007 1:00 pm 
Newbie

Joined: Sun Oct 28, 2007 1:21 am
Posts: 5
Hello again,

I've read some tutorials and I've found that was missing a element on the SET tag that is cascade="save-update".

After setting this tag is is throwning an exception that I show bellow:

Code:
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)




Does anyone knows how can I set the Id on the class manually?
Please I really need help. Can anyone say something, a guide line or something like that?


Many Thanks
Texugo


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.