我有一个名为refund_rule
mysql (version 5.5)的表。这是它的定义:
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 的回答,我对代码进行了以下更改:
(简要更改)
- 添加了 CharToBoolUserType 类
- 更改了RefundRule.hbm.xml
type
中 hbm-element: percent 的 -属性
(与上述更改相关的代码)
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. object
in或的类hashCode(Object object)
是什么?谁调用这个方法?
2. 方法应该怎么做?替换为并设置/放入. 在这种情况下:是类型,类型和类型?Boolean
String
public Object replace(Object original, Object target, Object owner)
original
target
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;
}
}