-->
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: getter method with parameters
PostPosted: Wed Aug 13, 2008 3:56 am 
Newbie

Joined: Fri May 09, 2008 12:12 am
Posts: 3
I have a table which has field names like catUtl1, catUtl2, catUtl3 etc.. which are differing only in the last digit. I wanted to create a hibernate class corresponding to that. Do I have to create getter and setters for each of the field names or there is a way to have a standard method that takes in parameters and fetches the right field based on that?


Thanks
Moni


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 13, 2008 5:14 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
My recommendation is to provide getters and setter for each field. There is also the possibility to specify a custom strategy by setting the "access" attribute of a <property> tag to the name of a class that implements org.hibernate.property.PropertyAccessor. I have case which implements a Map-like setter/getter.

In the class I have the following:
Code:
public String get(String name)
{
   return map.get(name);
}
public void set(String name, String value)
{
   map.put(name, value);
}


In my hbm.xml file:
Code:
<property
   name="first"
   column="first"
   type="string"
   access="my.PropertyAccessor"
/>
<property
   name="second"
   column="second"
   type="string"
   access="MyPropertyAccessor"
/>


Implementing a PropertyAccessor is not very difficult. It is only used to create a Getter and a Setter object for each property. You need to provide Getter and Setter implementations also.

Code:
public class MyPropertyAccessor
{
  public Getter getGetter(Class class, String property)
  {
     return new MyGetter(property);
   }
// ... and a similar getSetter()
}

public class MyGetter
{
  private String property;
  public MyGetter(String property)
  {
     this.property = property;
  }

  public Object get(Object target)
  {
      return ((MyObject)target).get(property);
   }
   // ... and some more methods
}

// ... and a similar MySetter class


The MyPropertyAccess.getGetter() and MyPropertyAccessor.getSetter() will be called once for each <property> in the hbm.xml file when Hibernate starts up. After that Hibernate will use the MyGetter.get() and MySetter.set() to get and set values.

Check the Hibernate API documentation (javadoc) for more information.


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.