-->
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: hibernate cache issue
PostPosted: Fri Nov 23, 2012 2:15 pm 
Newbie

Joined: Fri Nov 23, 2012 2:04 pm
Posts: 2
When a object has a collection and a element of this collection is removed or insert a new, the hibernate cache will not be updated and if it does a search of this object, the removed element of collection in object will appear, but it's wrong!!! cause was removed!!!, hibernate is not updating the own cache.
As a workaround, update manually the object collection.


my question is:
the behavior of hibernate is right in this situation?
how I get the object with a consistent state from the method get in hibernate?


bellow I posted a sample code of this situation,


Code:
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class Main {
   
   public static void main(String[] args) {
      SessionFactory sf = createSessionFactory();
      Session s = sf.openSession();
      
      Transaction tx = s.beginTransaction();
      try {
         Master master = new Master();
         master = (Master)s.merge(master);
         
         Item i = new Item();
         i.setMaster(master);
   
         s.merge(i);
         
         Master data = (Master)s.get(Master.class, master.getId());
         System.out.println(data.getItens().size());
      } finally {
         tx.rollback();
      }
   }
   
   private static SessionFactory createSessionFactory(){
      Configuration configuration = new Configuration();
      
      configuration.addAnnotatedClass(Master.class);
      configuration.addAnnotatedClass(Item.class);
      configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
      configuration.setProperty("hibernate.show_sql", "true");
      configuration.setProperty("hibernate.hbm2ddl.auto","create");
      configuration.setProperty("hibernate.connection.url", "jdbc:h2:mem:xxx;DB_CLOSE_DELAY=1000");
      configuration.setProperty("hibernate.connection.driver_class", "org.h2.Driver");
      
       ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();       
       SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
       return sessionFactory;
   }
}

@Entity
class Master {

   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   private Integer id;
   
   @OneToMany(mappedBy="master", fetch=FetchType.LAZY, cascade=CascadeType.ALL)
   private List<Item> itens = new ArrayList<Item>();

   public Integer getId() {
      return id;
   }

   public void setId(Integer id) {
      this.id = id;
   }

   public List<Item> getItens() {
      return itens;
   }

   public void setItens(List<Item> itens) {
      this.itens = itens;
   }
}

@Entity
class Item {

   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   private Integer id;
   
   @ManyToOne()
   @JoinColumn(name="master", referencedColumnName="id")
   private Master master;

   public Integer getId() {
      return id;
   }

   public void setId(Integer id) {
      this.id = id;
   }

   public Master getMaster() {
      return master;
   }

   public void setMaster(Master master) {
      this.master = master;
   }   
}


Top
 Profile  
 
 Post subject: Re: hibernate cache issue
PostPosted: Tue Nov 27, 2012 5:53 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hi Daniel,

on bidirectional relations you always must set both sides of the association (look at the manual).

Code:
i.setMaster(master);


is not enough, correct here is:

Code:

i.setMaster(master);       
master.getItens().add(i);


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.