簡體   English   中英

在MySQL中將CHAR(0)映射到Hibernate中的布爾值

[英]Mapping CHAR(0) in MySQL to boolean in Hibernate

. 我在mysql 有一個名為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`)
);

is named RefundRule . Hibernate 的相應類名為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>

was this: 由NetBeans 的向導生成的類是:

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;
    }

}

program. 我在它的setPercentage(boolean)isPercentage()添加了2個方法,並更改了setPercentage(String)方法,以便可以在我的Java 程序中將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)變量。

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

編輯添加於

按照@GreyBeardedGeek的回答,我對代碼進行了以下更改:

(簡要變化)

  1. 添加類CharToBoolUserType
  2. in RefundRule.hbm.xml 更改了RefundRule.hbm.xml中hbm-element的type -attribute:

(與上述更改相關的代碼)

  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> 

由於我希望在所有意義上都完整完成class CharToBoolUserType的代碼,因此我有以下問題:
1. hashCode(Object object)BooleanStringobject的類別是什么? 誰調用此方法?
2. public Object replace(Object original, Object target, Object owner)應該采取什么方法? original替換為target並將其設置/放置在owner 在這種情況下:是String類型的originalBoolean類型的targetRefundRule類型的owner嗎?

歡迎提出任何改進此代碼的建議。

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

僅供參考,類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;
    }

}

我相信您正在尋找Hibernate的UserType,它允許您提供自定義類型映射。

參見例如http://docs.jboss.org/hibernate/core/3.6/reference/zh-CN/html/types.html#types-custom

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM