-->
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.  [ 7 posts ] 
Author Message
 Post subject: Lookup table mapping strategy
PostPosted: Fri Jul 30, 2004 12:23 am 
Beginner
Beginner

Joined: Tue Jun 22, 2004 3:16 pm
Posts: 35
In our application, we have few lookup tables. They store static data and not changeable. What is the strategy to implement mapping? I use UserType which works fine. But it seems decoupling the database table and Java class. For example, if I have a table stores all addres types such as home address, mailing address ..., in my Typesave emnu class, I will have these instances independently. Is this the right desing?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 30, 2004 6:01 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
Map your reference (not changeable) data with mutable="false" and enable read-only caching for it, it is the best solution, 'case if you add new reference element you don't have to change any type-safe enumerations and it is simpler to implement. Here is an example of such class from my current project
Code:
   <class name="ru.xxx.BusinessAction" table="`BusinessAction`"
      batch-size="10" mutable="false">

      <cache usage="read-only"/>

      <id name="id" column="`BusinessActionId`" type="ru.xxx.hibernate.UniqueKeyType" access="field">
         <generator class="ru.incom.solaris.hibernate.UniqueKeyGenerator"/>
      </id>

      <property name="name" column="`BusinessActionName`" access="field"/>

   </class>


--
Leonid


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 30, 2004 12:17 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
And set outer-join=false on all associations to the cached entities


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 05, 2004 6:15 am 
Newbie

Joined: Thu Aug 05, 2004 2:24 am
Posts: 6
Location: Novosibirsk
I tryed release you technology but had some errors (or mistakes).

Could you write more examples: BusinessAction class and mapping for class that uses BusinessAction?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 05, 2004 6:39 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
igel wrote:
I tryed release you technology but had some errors (or mistakes).

Could you write more examples: BusinessAction class and mapping for class that uses BusinessAction?


BusinessAction.java:
Code:
public class BusinessAction implements Serializable
{
   private UniqueKey id = null;

   private String name;

   private transient String cachedToString = null;

   public UniqueKey getId()
   {
      return id;
   }

   public String getName()
   {
      return name;
   }

   public boolean equals(Object o)
   {
      if (this == o) return true;
      if (!(o instanceof BusinessAction)) return false;

      final BusinessAction businessAction = (BusinessAction) o;

      if (name != null ? !name.equals(businessAction.name) : businessAction.name != null) return false;

      return true;
   }

   public int hashCode()
   {
      return (name != null ? name.hashCode() : 0);
   }

   public String toString()
   {
      if (null == this.cachedToString)
      {
         StringBuffer result = new StringBuffer("BusinessAction@{")
               .append("id: ").append(id)
               .append(", name: ").append(name)
               .append("}");

         this.cachedToString = result.toString();
      }

      return this.cachedToString;

   }
}


BusinessPermission.java (class has BusinessAction association):
Code:
public class BusinessPermission implements Serializable, IModelObject
{
   public static final BusinessPermission EMPTY = new BusinessPermission();

   private UniqueKey id = null;

   private BusinessAction businessAction;

   private BusinessClass businessClass;

   /**
    * Lazily initialized, cached hashCode.
    * see item 8 of "Effective Jave".
    */
   private volatile int hashCode = 0;

   public BusinessPermission()
   {
   }

   public BusinessPermission(BusinessAction businessAction, BusinessClass businessClass)
   {
      if (null == businessAction)
         throw new NullPointerException("Argument businessAction is null!");

      if (null == businessAction.getId())
         throw new IllegalArgumentException("Argument businessAction.getId() is null!");

      if (null == businessClass)
         throw new NullPointerException("Argument businessClass is null!");

      if (null == businessClass.getId())
         throw new IllegalArgumentException("Argument businessClass.getId() is null!");

      this.businessAction = businessAction;
      this.businessClass = businessClass;
   }

   public UniqueKey getId()
   {
      return id;
   }

   public BusinessAction getBusinessAction()
   {
      return businessAction;
   }

   public BusinessClass getBusinessClass()
   {
      return businessClass;
   }

   public boolean equals(Object o)
   {
      if (this == o) return true;
      if (!(o instanceof BusinessPermission)) return false;

      final BusinessPermission businessPermission = (BusinessPermission) o;

      if (businessAction != null ? !businessAction.equals(businessPermission.businessAction) : businessPermission.businessAction != null) return false;
      if (businessClass != null ? !businessClass.equals(businessPermission.businessClass) : businessPermission.businessClass != null) return false;

      return true;
   }

   public int hashCode()
   {
      if (0 == hashCode)
      {
         int result = 17;
         result = 37 * result + (businessAction != null ? businessAction.hashCode() : 0);
         result = 37 * result + (businessClass != null ? businessClass.hashCode() : 0);
         hashCode = result;
      }

      return hashCode;
   }

   public String toString()
   {
      StringBuffer result = new StringBuffer("BusinessPermission@{")
         .append("id: ").append(id)
         .append(", businessAction: ").append(businessAction)
         .append(", businessClass: ").append(businessClass)
         .append("}");

      return result.toString();
   }
}


BusinessPermission mapping:
Code:
<hibernate-mapping>
   <class name="ru.xxx.common.security.BusinessPermission" table="`BusinessPermission`"
      batch-size="100" mutable="false">

      <cache usage="nonstrict-read-write"/>

      <id name="id" column="`BusinessPermissionId`" type="ru.xxx.hibernate.UniqueKeyType" access="field">
         <generator class="ru.xxx.hibernate.UniqueKeyGenerator"/>
      </id>

      <many-to-one name="businessAction" column="`BusinessActionId`" access="field" cascade="none"
         class="ru.xxx.common.security.BusinessAction" outer-join="false"/>

      <many-to-one name="businessClass" column="`BusinessClassId`" access="field" cascade="none"
         class="ru.xxx.common.security.BusinessClass" outer-join="false"/>

   </class>
</hibernate-mapping>


cache configuration (ehcache.xml):
Code:
<ehcache>
   <diskStore path="java.io.tmpdir"/>

   <defaultCache
      maxElementsInMemory="10000"
      eternal="false"
      timeToIdleSeconds="120"
      timeToLiveSeconds="120"
      overflowToDisk="false"
      />

   <cache name="ru.xxx.common.security.BusinessClass"
      maxElementsInMemory="10"
      eternal="true"
      timeToIdleSeconds="0"
      timeToLiveSeconds="0"
      overflowToDisk="false"
      />

   <cache name="ru.xxx.common.security.BusinessAction"
      maxElementsInMemory="10"
      eternal="true"
      timeToIdleSeconds="0"
      timeToLiveSeconds="0"
      overflowToDisk="false"
      />

   <cache name="ru.xxx.common.security.BusinessPermission"
      maxElementsInMemory="100"
      eternal="true"
      timeToIdleSeconds="600"
      timeToLiveSeconds="2400"
      overflowToDisk="false"
      />
</ehcache>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 05, 2004 6:44 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
Oops, I have found a bug in my current project :), should be eternal="false" for BusinessPermission cache
Code:
<cache name="ru.xxx.common.security.BusinessPermission"
      maxElementsInMemory="100"
      eternal="false"
      timeToIdleSeconds="600"
      timeToLiveSeconds="2400"
      overflowToDisk="false"
      />


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 05, 2004 6:48 am 
Newbie

Joined: Thu Aug 05, 2004 2:24 am
Posts: 6
Location: Novosibirsk
Thanks a lot.
I was mistaken. I thought you tell about other structure. I need save enumeration like this:

Code:
public class Side implements Serializable {
    public static final Side INTRUDER = new Side("INTRUDER");
    public static final Side GUARD = new Side("GUARD");
    public static final Side NEUTRAL = new Side("NEUTRAL");

    private String name;

    private Side() {
    }

    private Side(String name) {
        this.name = name;
    }

    public String toString() {
        return name;
    }
}

public class SyntheticEntity {

    private Side side;

    public void setSide(Side side) {
        this.side = side;
    }

    public Side getSide() {
            return side;
    }
}



I found one way, but it's look not so beautifully as I want: http://www.hibernate.org/172.html


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