我是Hibernate的新手。编写CompositeUserType。当我运行代码时,我收到错误。 财产
映射列数错误: 请帮帮我,我错过了什么?
我的CompositeUserType如下
public class EncryptedAsStringType implements CompositeUserType {
@Override
public String[] getPropertyNames() {
return new String[] { "stockId", "stockCode", "stockName","stockDescription" };
}
@Override
public Type[] getPropertyTypes() {
//stockId, stockCode,stockName,modifiedDate
return new Type[] {
Hibernate.INTEGER, Hibernate.STRING, Hibernate.STRING,Hibernate.STRING
};
}
@Override
public Object getPropertyValue(final Object component, final int property)
throws HibernateException {
Object returnValue = null;
final Stock auditData = (Stock) component;
if (0 == property) {
returnValue = auditData.getStockId();
} else if (1 == property) {
returnValue = auditData.getStockCode();
} else if (2 == property) {
returnValue = auditData.getStockName();
} return returnValue;
}
@Override
public void setPropertyValue(final Object component, final int property,
final Object setValue) throws HibernateException {
final Stock auditData = (Stock) component;
}
@Override
public Object nullSafeGet(final ResultSet resultSet,
final String[] names,
final SessionImplementor paramSessionImplementor, final Object paramObject)
throws HibernateException, SQLException {
//owner here is of type TestUser or the actual owning Object
Stock auditData = null;
final Integer createdBy = resultSet.getInt(names[0]);
//Deferred check after first read
if (!resultSet.wasNull()) {
auditData = new Stock();
System.out.println(">>>>>>>>>>>>"+resultSet.getInt(names[1]));
System.out.println(">>>>>>>>>>>>"+resultSet.getString(names[2]));
System.out.println(">>>>>>>>>>>>"+resultSet.getString(names[3]));
System.out.println(">>>>>>>>>>>>"+resultSet.getString(names[4]));
}
return auditData;
}
@Override
public void nullSafeSet(final PreparedStatement preparedStatement,
final Object value, final int property,
final SessionImplementor sessionImplementor)
throws HibernateException, SQLException {
if (null == value) {
} else {
final Stock auditData = (Stock) value;
System.out.println("::::::::::::::::::::::::::::::::"+auditData.getStockCode());
System.out.println("::::::::::::::::::::::::::::::::"+auditData.getStockDescription());
System.out.println("::::::::::::::::::::::::::::::::"+auditData.getStockId());
System.out.println("::::::::::::::::::::::::::::::::"+auditData.getStatus());
}
}
我的域类库存有五个属性。 (stockId,stockCode,StockName,Status,Stock 描述)
我需要将字段库存描述声明为复合字段类型。
private Integer stockId;
private String stockCode;
private String stockName;
private String status;
private String stockDescription;
//Constructors
@Column(name = "STOCK_CC", unique = true, nullable = false, length = 20)
@Type(type="com.mycheck.EncryptedAsStringType")
@Columns(columns = { @Column(name="STOCK_ID"),
@Column(name="STOCK_CODE"),
@Column(name="STOCK_NAME")
})
public String getStockDescription() {
return stockDescription;
}
}
当我尝试执行Stock插入时。我收到错误错误创建名称为
的bean'的sessionFactory'在类路径资源[spring / config /../ database / Hibernate.xml]中定义:
调用init方法失败。嵌套异常是org.hibernate.MappingException:
属性映射列数错误:com.stock.model.Stock.stockDescription类型:
com.mycheck.EncryptedAsStringType
我哪里错了?
答案 0 :(得分:2)
可以从代码示例和原始问题的评论中提取答案,但为了节省每个人的阅读,我编写了一个快速摘要。
如果声明将{1}映射到n列的CompositeUserType
,则必须在@Columns
注释之外的@Type
中声明n列。例如:
public class EncryptedAsStringType implements CompositeUserType {
@Override
public String[] getPropertyNames() {
return new String[] { "stockId", "stockCode", "stockName","stockDescription" };
}
// ...
}
此CompositeUserType
映射到4个单独的列,因此必须声明4个单独的@Column
注释:
@Type(type="com.mycheck.EncryptedAsStringType")
@Columns(columns = {
@Column(name="STOCK_ID"),
@Column(name="STOCK_CODE"),
@Column(name="STOCK_NAME"),
@Column(name="STOCK_DESCRIPTION")
})
public String getStockDescription() {
return stockDescription;
}
就是这样,Hibernate很高兴。