tags:

views:

53

answers:

1

Java bean Format:

public class Format extends HibernateBean implements Serializable {

  private static final Logger log = Logger.getLogger(Format.class);
  private Long id;
  private boolean isQualifyFormat;

  ...other fields

  public Long getId() {
    return id;
  }

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

  **//static method, it's exposed to a HttpServlet**
  public static Format getQualifyFormat() throws HibernateException {        
    log.debug("getQualifyFormat");
    ...
  }

  /**
   * @hibernate.property column="df_isqualifyinglistformat" type="boolean"
   * @return boolean
   */
  public boolean isQualifyFormat() {
     log.debug ("isQualifyFormat");
     return isQualifyingListFormat;
  }

  public void setQualifyFormat(boolean qualifyFormat) {
      isQualifyingListFormat = qualifyingListFormat;
  }  
  ...

 }

Hibernate mapping file, Format.hbm.xml

 <hibernate-mapping>
 <class
    name="Format"
    table="dbo.mc_distribution_format"
    schema="data"    
  >

  <id
      name="id"
      column="df_formatid"
      type="java.lang.Long"
   >
      <generator class="native"></generator>
   </id>      
   <property
       name="qualifyFormat"
       type="boolean"
       update="true"
       insert="true"
       column="df_isqualifyformat"
    />

In theoretically, method isQualifyFormat of bean Format should be invoked in Hibernate persistense process for bean Format. As far as I know, the getter of Boolean property can be pattern is- or get-. Method is- has the priority if both are available in the bean.

But, it works well in most cases except case: If there has any code refers to the static method, Format.getQualifyFormat, in the same jar package. The above getQualifyFormat method is invoked instead somehow even it's static and there already has the is-.

This happens when i upgraded my code to JDK 1.5. It was good in JDK 1.4. I guess this might be caused by issue of reflection improvement in JDk 1.5, does jdk/jre parses code and finds reference of static method getQualifyFormat in compiling time, then caches it as the default getter of property qualifyFormat?

Any ideas?

+1  A: 

If you want things to work properly, you must use the appropriate conventions.

If you field name is xyz

  • the accessor ("getter") is called getXyz() (it may and should also be called isXyz() iff xyz is of type boolean)
  • the mutator ("setter") is called setXyz() and must return void

If you don't adhere to these conventions, Java bean methods won't work, and those are used by almost every framework there is out there.

Also, having a static getXyz() method next to an isXyz() method is asking for trouble, to say the least. I don't know if it works, but it's awfully confusing.

In my personal opinion, a class that has bean properties should not have any other methods named get*, set* or is* if they are not getters or setters to make the method intents clearer.

seanizer
Thank you! I agreed with you suggestion. But, the point is, I need to figure out how it gets wrong in my case. My field isQualifyFormat has the setter method setQualifyFormat and getter method isQualifyFormat. Why it hits the static method getQualifyFormat instead of isQualifyFormat when there has any code refers to the static methos Format.getQualifyFormat?
Rick Wang
My guess is that hibernate looks for get*() methods first and uses is*() methods as fallback only. and I'm not sure they check for non-static methods (I'm not even sure the bean spec requires methods to be non-static, although they should be). so: keep things safe, avoid misunderstandings.
seanizer
Does it matter? As pointed out, your code IS NOT following the Java Beans spec. *edit* This is in relation to the OP and not seanizer.
hbunny