-->
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: Strange behaviour with Hibernate and flushing
PostPosted: Sun May 22, 2011 5:48 am 
Newbie

Joined: Sun May 22, 2011 5:21 am
Posts: 3
Hello everyone.

I have entity Parent with set of children like this:
Code:
@Entity
public class Parent extends BaseEntity {

           ***other fields***

   @OneToMany(cascade = CascadeType.ALL)
   @JoinTable(name = "PARENT_CHILD", joinColumns = { @JoinColumn(name = "PARENT_ID") }, inverseJoinColumns = { @JoinColumn(name = "CHILD_ID") })
   private Set<Child> children = new HashSet<Child>(0);
           
          ***setters/getters***   
}

Code:
@Entity
public class Child extends BaseEntity {
           ***other fields***           
          ***setters/getters***   
}

I use GenericDAO to persist and load data.

Let say:
Code:
Parent parent1 = new Parent();
parent1.setName("parentName");
parentDao.save(parent1);

Persists parent1 to database. Everything is fine.

Later I try to add child for this parent:
Code:
Parent managedParent = parentDao.findParentByName("testName");

Child child1 = new Child();
child1.setName("childName");

managedParent.getChildren().add(child1);

if (!dryRun) {
    parentDao.update(managedParent);
} else {
    // do not persist any changes if this is only dry run
}

So, even when dryRun is TRUE, child persists to database after flush.

Any idea, how to avoid this auto flushing?

I tried to set flushType like this:
Code:
entityManager.setFushMode(FlushModeType.COMMIT);

but this did not help.


Please help me :)


Top
 Profile  
 
 Post subject: Re: Strange behaviour with Hibernate and flushing
PostPosted: Sun May 22, 2011 8:12 am 
Newbie

Joined: Sun May 22, 2011 5:21 am
Posts: 3
I solved the issue by clearing session if it's dry run like this:
Code:
...

if (!dryRun) {
    parentDao.update(managedParent);
} else {
    parentDao.getEntityManager().clear();
}


I don't know if this is correct solution, so if anyone can review this, please provide a feedback.


Top
 Profile  
 
 Post subject: Re: Strange behaviour with Hibernate and flushing
PostPosted: Mon May 23, 2011 9:54 am 
Newbie

Joined: Thu Apr 21, 2011 8:59 am
Posts: 14
Hi meduolis,

Any changes that are made to entities inside a persistence context (in a transaction) will get written to the database either due to to auto flush or at the transaction commit time.
So, the child addition which I believe is happening inside a transaction will be committed to the database irrespective of flush mode is AUTO or COMMIT.
Clearing the entity manager will discard all the changes and hence no changes will be written to the db at the transaction commit time and I do not see any issues with this approach.
Another solution that I can suggest would be to avoid using transactions for the function block which contains the child addition part but use the transactions for just the DAO (like parentDao.update).

_________________
Lokesh, C
( SCBCD 5, CCENT, SCJP 5 )


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.