使用hibernatetemplate时遇到麻烦。
我正在将旧版应用程序从旧版本的spring / hibernate / apache lucene升级到spring(4.x)/ hibernate(5.x)的最新版本
我们正在使用hibernatetemplate查找产品,该产品是从数据库中检索到的,其中一个字段使用的是未填充的自定义用户类型映射。
这是我的java类
@Indexed
public final class ProductImpl extends Primordial implements MutableProduct {
@DocumentId
private long id;
@Field(index = Index.NO, store = Store.YES, bridge = @FieldBridge(impl = ToStringBridge.class))
private Sku sku;
.....
}
下面是我的hbm xml-在其中未从表中检索Sku。我可以打印所有其他值。
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-access="field">
<typedef name="productStatus" class="com.example.core.store.hibernate.orm.EnumUserType">
<param name="enumClass">com.example.core.model.enums.ProductStatus</param>
</typedef>
<class name="com.example.core.model.type.product.ProductImpl" table="pct_product">
<cache usage="transactional" />
<id name="id" column="prd_id">
<generator class="sequence" />
</id>
<property name="acccCheckedDate" column="prd_accc_checked_date" type="date" />
<property name="supplierProductCode" column="prd_supplier_product_code" length="4000" />
<property name="sku" column="prd_sku" not-null="true" unique="true" type="com.example.core.store.hibernate.orm.SkuUserType" length="100" />
<property name="packageName" column="prd_package_name" not-null="true" length="4000" />
<property name="packageDescription" column="prd_package_desc" length="4000" />
public final class SkuUserType {
private static final int[] SQL_TYPES = new int[]{VARCHAR};
public int[] sqlTypes() {
return SQL_TYPES;
}
public Class returnedClass() {
return Sku.class;
}
public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
String value = rs.getString(names[0]);
checkNotEmpty(value);
return sku(value);
}
public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
checkNotNull(value);
int sku = toInt(value);
st.setString(index, ""+sku);
}
private int toInt(Object value) {
if (value instanceof Sku) return ((Sku) value).getValue();
if (value instanceof String) return parseInt((String) value);
if (value instanceof Integer) return (Integer) value;
throw new IllegalStateException();
}
private Sku sku(String value) {
int sku = parseInt(value);
return new SkuImpl(sku);
}
}