-->
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.  [ 6 posts ] 
Author Message
 Post subject: Problem with lock() when persisting parent/child
PostPosted: Thu Dec 15, 2005 7:43 am 
Beginner
Beginner

Joined: Thu Sep 22, 2005 7:22 am
Posts: 21
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.1

Mapping documents:

Code:
@Entity(access=AccessType.FIELD)
public class User implements Serializable {

   @Id(generate = GeneratorType.AUTO)
   private Long id;
   @ManyToOne(cascade=CascadeType.ALL)  //TODO Is this really necessary
   @NotNull
   private Collective collective;
   
   public Collective getCollective() {
      return collective;
   }
   public void setCollective(Collective collective) {
      this.collective = collective;
   }
}


Code:
@Entity(access=AccessType.FIELD)
public class Collective {
   
   @Id(generate = GeneratorType.AUTO)
   private Long id;

   private String name;
   @OneToMany(cascade=CascadeType.ALL,
         fetch = FetchType.LAZY,
         mappedBy="collective")
   @OrderBy("name")
   private Set<User> users;

   @OneToMany(cascade=CascadeType.ALL,
         fetch = FetchType.LAZY,
         mappedBy="collective")
   @OrderBy("name")
   private Set<Category> categories;

   public Set<User> getUsers() {
      return users;
   }

   public void setUsers(Set<User> users) {
      this.users = users;
   }

   public Set<Category> getCategories() {
      return categories;
   }
   
   public void addCategory(Category category) {
      category.setCollective(this);
      categories.add(category);
   }
   
}



Code between sessionFactory.openSession() and session.close():
Code:
User user = getUserFromHTTPSession();
session.lock(user, LockMode.NONE); //reconnecting user.

Collective collective = user.getCollective();
Category category = new Category();
category.setIsDeleted(false);
category.setName(getNewCategoryName());
user.getCollective().addCategory(category);
         
session.flush();


for(Category currCategory : user.getCollective().getCategories()) {
   System.out.println("category: " + currCategory.getName());
}


The first time try to reconnect user to the session (with lock()) I get
org.hibernate.HibernateException: reassociated object has dirty collection
The thing is that I have followed the Parent/Child example in the Hibernate manual (Chapter 21) and I still don't get it to work :(

_________________
/ted


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 15, 2005 10:44 am 
Beginner
Beginner

Joined: Thu Sep 22, 2005 7:22 am
Posts: 21
bump...
come on! please help me out here..
If I am way off, please tell me.

_________________
/ted


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 15, 2005 10:47 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
try merge

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 15, 2005 11:00 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Code:
User user = 
session.load(User.class, getUserIdFromHTTPSession()); .

Collective collective = user.getCollective();
Category category = new Category();
category.setIsDeleted(false);
category.setName(getNewCategoryName());
user.getCollective().addCategory(category);
         
session.flush();


for(Category currCategory : user.getCollective().getCategories()) {
   System.out.println("category: " + currCategory.getName());
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 15, 2005 11:17 am 
Beginner
Beginner

Joined: Thu Sep 22, 2005 7:22 am
Posts: 21
baliukas wrote:
Code:
User user = 
session.load(User.class, getUserIdFromHTTPSession()); .

Collective collective = user.getCollective();
Category category = new Category();
category.setIsDeleted(false);
category.setName(getNewCategoryName());
user.getCollective().addCategory(category);
         
session.flush();


for(Category currCategory : user.getCollective().getCategories()) {
   System.out.println("category: " + currCategory.getName());
}


why don't you think I should re-use the already loaded object?
I mean there is a session.lock() just for that reason.
is it not good practices to reuse objects instead of reloading them?
actually that brings up a new question.. what happens if I reconnect an objects (with lock()) that has been altered in the database. will those changes propagate to the object?

_________________
/ted


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 15, 2005 11:32 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
You can store persistent objects in http session if you can make it thread safe, have more memory than database size and tolerate stale data.


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