将MySQL中的CHAR(0)映射到Hibernate中的布尔值

时间:2013-11-14 11:08:26

标签: java mysql hibernate

我在mysql (版本5.5)中有一个名为refund_rule的表。这是它的定义:

CREATE TABLE `refund_rule` (
  `ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `PERCENTAGE` char(0) DEFAULT NULL COMMENT 'BOOLEAN shortcut.NULL<=>false,EMPTY<=>true',
  `DEDUCTION_AMOUNT` int(10) unsigned DEFAULT NULL,
  PRIMARY KEY (`ID`)
);

Hibernate (版本3.2)中的相应类名为RefundRule。 HBM文件如下所示:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="hibernatesample.dao.RefundRule" table="refund_rule" catalog="back_end_proc">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="identity" />
        </id>
        <property name="percentage" type="string">
            <column name="PERCENTAGE" length="0">
                <comment>BOOLEAN shortcut.NULL&lt;=&gt;false,EMPTY&lt;=&gt;true</comment>
            </column>
        </property>
        <property name="deductionAmount" type="java.lang.Integer">
            <column name="DEDUCTION_AMOUNT" />
        </property>
    </class>
</hibernate-mapping>

NetBeans (版本7.0)中向导生成的类是:

public class RefundRule  implements java.io.Serializable {

     private Integer id;
     private String percentage;
     private Integer deductionAmount;

    public CancellationRule() {
    }

    public CancellationRule(String percentage, Integer deductionAmount) {
       this.percentage = percentage;
       this.deductionAmount = deductionAmount;
    }

    public Integer getId() {
        return this.id;
    }

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

    public String getPercentage() {
        return this.percentage;
    }

    public void setPercentage(String percentage) {
        this.percentage = percentage;
    }

    public Integer getDeductionAmount() {
        return this.deductionAmount;
    }

    public void setDeductionAmount(Integer deductionAmount) {
        this.deductionAmount = deductionAmount;
    }

}

我在其中添加了另外两种方法setPercentage(boolean)&amp; isPercentage(),并更改了方法setPercentage(String),以便我可以在我的Java (版本1.6)程序中将String对象用作boolean

public class RefundRule  implements java.io.Serializable {

    .
    .
    .

    public String getPercentage() {
        return this.percentage;
    }

    public void setPercentage(String percentage) {
        this.percentage = percentage==null?null:"";
    }

    .
    .
    .

    public void setPercentage(boolean percentage){
        setPercentage(percentage?"":null);
    }

    public boolean isPercentage(){
        return percentage!=null;
    }

}

我的问题是:

有什么方法可以只保留两种方法:setPercentage(boolean)isPercentage(),并将boolean percentage变量映射到mysql中的PERCENTAGE CHAR(0)变量。

=============================================== ====================

2013-11-23

上添加

编辑

根据@GreyBeardedGeek的回答,我在代码中进行了以下更改:

(简要说明)

  1. 添加了Class CharToBoolUserType
  2. 更改了RefundRule.hbm.xml中百分比的type - 属性
  3. (与上述变更相关的代码)

    1. CharToBoolUserType类:

      import java.io.Serializable;
      import java.sql.PreparedStatement;
      import java.sql.ResultSet;
      import java.sql.SQLException;
      import java.sql.Types;
      
      import org.hibernate.HibernateException;
      import org.hibernate.usertype.UserType;
      
      public class CharToBoolUserType implements UserType {
      
          private static final int[] SQL_TYPES = {Types.CHAR};
      
          @Override
          public Object assemble(Serializable serializable, Object object) throws HibernateException {
              return serializable;
          }
      
          @Override
          public Object deepCopy(Object object) throws HibernateException {
              return object;
          }
      
          @Override
          public Serializable disassemble(Object value) throws HibernateException {
              return (Serializable) value;
          }
      
          @Override
          public boolean equals(Object x, Object y) throws HibernateException {
             if (x == y) {
                 return true;
             } else if (x == null || y == null) {
                 return false;
             } else {
                 return x.equals(y);
             }
          }
      
          @Override
          public boolean isMutable() {
              return false;
          }
      
          @Override
          public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner) throws HibernateException, SQLException {
              return resultSet.getObject(names[0]) != null;
          }
      
          @Override
          public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index) throws HibernateException, SQLException {
              preparedStatement.setObject(index, ((Boolean) value).booleanValue() ? "" : null);
          }
      
          @Override
          public Object replace(Object original, Object target, Object owner) throws HibernateException {
              return original;
          }
      
          @Override
          public Class returnedClass() {
              return boolean.class;
          }
      
          @Override
          public int[] sqlTypes() {
              return SQL_TYPES;
          }
      
          @Override
          public int hashCode(Object object) throws HibernateException {
              if (object == null) {
                  return 0;
              }
              // is `object` a String ? Or boolean?
              return 1;
          }
      }
      
    2. 文件RefundRule.hbm.xml:

      <?xml version="1.0"?>
      <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
      "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
      <hibernate-mapping>
          <class name="hibernatesample.dao.RefundRule" table="refund_rule" catalog="back_end_proc">
              <id name="id" type="java.lang.Integer">
                  <column name="ID" />
                  <generator class="identity" />
              </id>
              <property name="percentage" type="hibernatesample.dao.CharToBoolUserType">
                  <column name="PERCENTAGE" length="0">
                      <comment>BOOLEAN shortcut.NULL&lt;=&gt;false,EMPTY&lt;=&gt;true</comment>
                  </column>
              </property>
              <property name="deductionAmount" type="java.lang.Integer">
                  <column name="DEDUCTION_AMOUNT" />
              </property>
          </class>
      </hibernate-mapping>
      
    3. 由于我希望class CharToBoolUserType的代码在所有意义上都是完整的,我有以下问题:
      1. objecthashCode(Object object)BooleanString的课程是什么?谁叫这种方法?
      2. public Object replace(Object original, Object target, Object owner)方法应该做什么?将original替换为target并将其设置/放入owner。在这种情况下:original类型为Stringtarget类型为Booleanowner类型为RefundRule

      欢迎任何改进此代码的建议。

      =============================================== ==

      仅供参考,RefundRule类现在就像这样:

      public class RefundRule  implements java.io.Serializable {
      
           private Integer id;
           private boolean percentage;
           private Integer deductionAmount;
      
          public RefundRule() {
          }
      
      
          public RefundRule(boolean percentage, Integer deductionAmount) {
              this.percentage = percentage;
              this.deductionAmount = deductionAmount;
          }
      
          public Integer getId() {
              return this.id;
          }
      
          public void setId(Integer id) {
              this.id = id;
          }
      
          public Integer getDeductionAmount() {
              return this.deductionAmount;
          }
      
          public void setDeductionAmount(Integer deductionAmount) {
              this.deductionAmount = deductionAmount;
          }
      
          public void setPercentage(boolean percentage){
              this.percentage=percentage;
          }
      
          public boolean isPercentage(){
              return percentage;
          }
      
      }
      

1 个答案:

答案 0 :(得分:1)

我相信您正在寻找Hibernate的UserType,它允许您提供自定义类型映射。

例如,请参阅http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/types.html#types-custom