-->
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.  [ 2 posts ] 
Author Message
 Post subject: How can I prevent saving the duplicate rows.
PostPosted: Tue Sep 28, 2004 11:19 am 
Beginner
Beginner

Joined: Tue Sep 21, 2004 1:03 pm
Posts: 21
Hi ,

I'm facing a problem when trying to save the persistent class and it's inserting the duplicate rows after generating the primary key.

I don't have primary key with the object , that I can load and comapre with the existing so implementing the equals and hashcode method is ruled out.

In the code I'm trying to get the record based on the other parameter and if it's there then setting isPersisted true and trying to not save it again.

Could it be problem because , I'm getting the object from a list and doing all the iteration in one session.

Appreciate any help.

Thanks & Regards,
Irfan





Hibernate version:2.1.6

Mapping documents:<?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.lastminute.sybil.promotion.instance.DefaultPromotion" table="promo">
<id name="ID">
<generator class="com.lastminute.sybil.promotion.persistence.hibernate.V2HibernateIDGenerator">
<param name="table">promo</param>
<param name="maxCachedIDs">100</param>
</generator>
</id>
<property name="promotionTypeLayerID" column="promo_type_layer_id" type="int" />
<joined-subclass name="com.lastminute.sybil.promotion.instance.DefaultDiscountStayPromotion" table="promo_discount_stay">
<key column="promo_id"/>
<property name="discount" column="discount" type="float" />
<property name="fullPriceNights" column="full_price_nights" type="int" />
<property name="discountPriceNights" column="discount_nights" type="int" />
</joined-subclass>
</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

Fields: protected static final String FIND_PROMOTION =
" from DefaultDiscountStayPromotion as promotion where promotion.discount = ? and promotion.fullPriceNights = ? and promotion.discountPriceNights = ?";

protected static final String FIND_PROMOTION_TYPE_LAYER =
"from DefaultPromotionTypeLayer as promoTypeLayer where promoTypeLayer.promotionTypeID = ? and promoTypeLayer.promotionLayerID = ?";
Session hibernateSession = openSession();
Transaction transaction = null;

PromotionRuleAuditEntry auditEntry;
float discount =0;
int fullPriceNights=0 ;
int discountPriceNights =0 ;
Promotion prom = promotionRule.getPromotion();

try{
transaction = hibernateSession.beginTransaction();

if (prom instanceof DefaultDiscountStayPromotion){

discount = ((DefaultDiscountStayPromotion)prom).getDiscount();
fullPriceNights = ((DefaultDiscountStayPromotion)prom).getFullPriceNights();
discountPriceNights =((DefaultDiscountStayPromotion)prom).getDiscountPriceNights();
}
List promo = hibernateSession.find(FIND_PROMOTION, new Object[]{new Double(discount),new Integer(fullPriceNights),
new Integer(discountPriceNights)},new Type[]{Hibernate.DOUBLE,Hibernate.INTEGER,Hibernate.INTEGER});

if(promo.size() >0){
for (Iterator i= promo.iterator();i.hasNext();){
Promotion promotion =(Promotion)i.next();
promotionRule.getPromotion().setID(promotion.getID());
}
}

int promoTypeID = prom.getPromotionType().getID();
int promoLayerID= prom.getPromotionLayer().getID();
int promoTypeLayerID =-1;

List promoTypeLayer = hibernateSession.find(FIND_PROMOTION_TYPE_LAYER, new Object[]{new Integer(promoTypeID),
new Integer(promoLayerID)},new Type[]{Hibernate.INTEGER,Hibernate.INTEGER});

for (Iterator i = promoTypeLayer.iterator();i.hasNext();){
PromotionTypeLayer typeLayer = (PromotionTypeLayer) i.next();
promoTypeLayerID = typeLayer.getID();
}

if(! promotionRule.getPromotion().isPersisted()){
promotionRule.getPromotion().setPromotionTypeLayerID(promoTypeLayerID);
hibernateSession.save(promotionRule.getPromotion());
}
hibernateSession.save(promotionRule);
auditEntry = new DefaultPromotionRuleAuditEntry(PromotionRuleAuditEntry.CREATED, promotionRule, user.getId());

hibernateSession.save(auditEntry);

transaction.commit();
}
catch(Exception e){
if(transaction != null){
transaction.rollback();
}

throw new PersistenceException("Unable to store a new PromotionRule", e);
}
finally{
closeSession(hibernateSession);
}



Full stack trace of any exception that occurs:

Name and version of the database you are using:Informix 9.1.4

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject: Re: How can I prevent saving the duplicate rows.
PostPosted: Tue Nov 09, 2004 6:35 am 
Beginner
Beginner

Joined: Tue Sep 21, 2004 1:03 pm
Posts: 21
Hi ,

I put the logic to prevent the saving duplicate rows as I was not getting identifier with the object to persist so I 'm comparing each fields value and then deciding to insert it or not.

Thanks & Regards,
Irfan

IrfanAnsari wrote:
Hi ,

I'm facing a problem when trying to save the persistent class and it's inserting the duplicate rows after generating the primary key.

I don't have primary key with the object , that I can load and comapre with the existing so implementing the equals and hashcode method is ruled out.

In the code I'm trying to get the record based on the other parameter and if it's there then setting isPersisted true and trying to not save it again.

Could it be problem because , I'm getting the object from a list and doing all the iteration in one session.

Appreciate any help.

Thanks & Regards,
Irfan





Hibernate version:2.1.6

Mapping documents:<?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.lastminute.sybil.promotion.instance.DefaultPromotion" table="promo">
<id name="ID">
<generator class="com.lastminute.sybil.promotion.persistence.hibernate.V2HibernateIDGenerator">
<param name="table">promo</param>
<param name="maxCachedIDs">100</param>
</generator>
</id>
<property name="promotionTypeLayerID" column="promo_type_layer_id" type="int" />
<joined-subclass name="com.lastminute.sybil.promotion.instance.DefaultDiscountStayPromotion" table="promo_discount_stay">
<key column="promo_id"/>
<property name="discount" column="discount" type="float" />
<property name="fullPriceNights" column="full_price_nights" type="int" />
<property name="discountPriceNights" column="discount_nights" type="int" />
</joined-subclass>
</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

Fields: protected static final String FIND_PROMOTION =
" from DefaultDiscountStayPromotion as promotion where promotion.discount = ? and promotion.fullPriceNights = ? and promotion.discountPriceNights = ?";

protected static final String FIND_PROMOTION_TYPE_LAYER =
"from DefaultPromotionTypeLayer as promoTypeLayer where promoTypeLayer.promotionTypeID = ? and promoTypeLayer.promotionLayerID = ?";
Session hibernateSession = openSession();
Transaction transaction = null;

PromotionRuleAuditEntry auditEntry;
float discount =0;
int fullPriceNights=0 ;
int discountPriceNights =0 ;
Promotion prom = promotionRule.getPromotion();

try{
transaction = hibernateSession.beginTransaction();

if (prom instanceof DefaultDiscountStayPromotion){

discount = ((DefaultDiscountStayPromotion)prom).getDiscount();
fullPriceNights = ((DefaultDiscountStayPromotion)prom).getFullPriceNights();
discountPriceNights =((DefaultDiscountStayPromotion)prom).getDiscountPriceNights();
}
List promo = hibernateSession.find(FIND_PROMOTION, new Object[]{new Double(discount),new Integer(fullPriceNights),
new Integer(discountPriceNights)},new Type[]{Hibernate.DOUBLE,Hibernate.INTEGER,Hibernate.INTEGER});

if(promo.size() >0){
for (Iterator i= promo.iterator();i.hasNext();){
Promotion promotion =(Promotion)i.next();
promotionRule.getPromotion().setID(promotion.getID());
}
}

int promoTypeID = prom.getPromotionType().getID();
int promoLayerID= prom.getPromotionLayer().getID();
int promoTypeLayerID =-1;

List promoTypeLayer = hibernateSession.find(FIND_PROMOTION_TYPE_LAYER, new Object[]{new Integer(promoTypeID),
new Integer(promoLayerID)},new Type[]{Hibernate.INTEGER,Hibernate.INTEGER});

for (Iterator i = promoTypeLayer.iterator();i.hasNext();){
PromotionTypeLayer typeLayer = (PromotionTypeLayer) i.next();
promoTypeLayerID = typeLayer.getID();
}

if(! promotionRule.getPromotion().isPersisted()){
promotionRule.getPromotion().setPromotionTypeLayerID(promoTypeLayerID);
hibernateSession.save(promotionRule.getPromotion());
}
hibernateSession.save(promotionRule);
auditEntry = new DefaultPromotionRuleAuditEntry(PromotionRuleAuditEntry.CREATED, promotionRule, user.getId());

hibernateSession.save(auditEntry);

transaction.commit();
}
catch(Exception e){
if(transaction != null){
transaction.rollback();
}

throw new PersistenceException("Unable to store a new PromotionRule", e);
}
finally{
closeSession(hibernateSession);
}



Full stack trace of any exception that occurs:

Name and version of the database you are using:Informix 9.1.4

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.