我在mysql (version 5.5)中有一個名爲refund_rule
的表。下面是它的定義:將MySQL中的CHAR(0)映射到休眠中的布爾值
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<=>false,EMPTY<=>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;
}
}
我加入2-多種方法在它setPercentage(boolean)
& 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()
,並映射在MySQL中boolean percentage
變量PERCENTAGE CHAR(0)
變量。
==============================================加入=====================
編輯上 2013年11月23日
繼@GreyBeardedGeek答案,我做了以下代碼中的變化:
(變化簡短)
- 添加的類CharToBoolUserTy PE
- 改變了
type
-attribute HBM-元件的:百分比在RefundRule.hbm.xml
(代碼與上述的變化)
類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; } }
該文件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<=>false,EMPTY<=>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)
,Boolean
或String
類的object
?誰稱這種方法?
2.方法public Object replace(Object original, Object target, Object owner)
應該做什麼?用target
替換original
並將其設置/放入owner
。在這種情況下:original
的String
類型,target
的Boolean
類型和owner
的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;
}
}