Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: 2nd Level Cache does not work for complex objects
PostPosted: Thu May 03, 2012 5:15 am 
Newbie

Joined: Fri Apr 15, 2011 3:19 am
Posts: 4
I'm currently trying to understand/get working the 2nd Level Cache. Currently I can get simple objects to be cached (simple being objects without relations to other model objects). But, whenever I try to cache a complex object, it seems to ignore it entirely.

The model does have some complexity. So a quick bit about the code:
Code:
@Entity
@Cache(region="cache.country", usage = CacheConcurrencyStrategy.READ_ONLY)
@Cacheable
public class Country extends DatabaseEntityBase<Integer>{
   private static final long serialVersionUID = 1L;

   @Id
   private Integer countryCode;

   @Column(length=2, nullable=false, unique=true)
   private String iso3166_1_alpha2;
   
   @Column(length=3, nullable=false, unique=true)
   private String iso3166_1_alpha3;
   
   @Column(nullable=false)
   private String localName;
   
   @Column(nullable=false)
   private String globalName;

        // constructors and getters/setters

This object does get cached, and is really not an issue. The following two never get cached though:
Code:
@Entity
@Cache(region="cache.municipality", usage = CacheConcurrencyStrategy.TRANSACTIONAL)
@Cacheable
@Table(uniqueConstraints=@UniqueConstraint(columnNames= {"municipalityCode", "countryCode"}))
public class Municipality extends Organization {
   private static final long serialVersionUID = 1L;
   
   @Column(nullable=false)
   private Integer municipalityCode;
   
   @ManyToOne
   @JoinColumn(name="countryCode", nullable=false)
   @Cache(region="cache.country", usage = CacheConcurrencyStrategy.TRANSACTIONAL)
   private Country country;
   
   @OneToMany(mappedBy="municipality")
   @Cache(region="cache.street", usage = CacheConcurrencyStrategy.TRANSACTIONAL)
   private Set<Street> streets;
   
   @ManyToMany
   @JoinTable(name="MunicipalityFieldSectorRelations",
       joinColumns=@JoinColumn(name="municipality_id"),
       inverseJoinColumns=@JoinColumn(name="fieldsector_id")
   )
   @Cache(region="cache.fieldSector", usage = CacheConcurrencyStrategy.TRANSACTIONAL)
   private Set<FieldSector> fieldSectors;
   
   @OneToMany(mappedBy="municipality")
   @Cache(region="cache.estate", usage = CacheConcurrencyStrategy.TRANSACTIONAL)
   private Set<Estate> estates = new HashSet<>();
   
   protected Municipality() {
      // Default null-args constructor, required by Hibernate!
   }

        // constructors and getters/setters

and lastly...
Code:
@Entity
@Cache(region="cache.estate", usage = CacheConcurrencyStrategy.TRANSACTIONAL)
@Cacheable
public class Estate extends DatabaseEntityBase<Long> {
   private static final long serialVersionUID = 1L;

   @Id
   @GeneratedValue(strategy=GenerationType.IDENTITY)
   private Long id;
   
   /**
    * Max. 6 digits
    */
   private Integer estateNumber;
      
   @ManyToOne
   private Estate parent;

   @ManyToMany
   @JoinTable(name="EstateAddressRelations",
       joinColumns=@JoinColumn(name="estate_id"),
       inverseJoinColumns=@JoinColumn(name="address_id")
   )
   private Set<Address> addresses;
   
   @ManyToMany
   @JoinTable(name="EstateFieldRelations",
       joinColumns=@JoinColumn(name="estate_id"),
       inverseJoinColumns=@JoinColumn(name="field_id")
   )
   @Cache(region="cache.field", usage = CacheConcurrencyStrategy.TRANSACTIONAL)
   private Set<Field> fields;
   
   @ManyToOne(optional=false)
   @Cache(region="cache.municipality", usage = CacheConcurrencyStrategy.TRANSACTIONAL)
   private Municipality municipality;

        // constructors and getters/setters
}

I'm loading the properties for hibernate programatically with the following snippet:
Code:
      addProperty("hibernate.cache.use_second_level_cache", Boolean.TRUE.toString());
      addProperty("hibernate.cache.use_query_cache", Boolean.TRUE.toString());
      addProperty("hibernate.cache.region.factory_class", regionFactory.getName());
      addProperty("hibernate.cache.infinispan.statistics", Boolean.TRUE.toString());
      addProperty("hibernate.cache.infinispan.cfg", infinispanConfigPath);


I'm specifically saving each element separately, thus the country, municapality and the estate are each being saved. The estate contains the municipality, and the municipality contains the country. But, always only the country is persisted. Is this the expected behavior do to my current setup? How can I get the estate and municipality to also be persisted?

Any help is appreciated, thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

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.