Spring Hibernate CompositeUserType映射的列数错误
我刚开始冬眠。写一个复合字体。当我运行代码时,我得到一个错误。 财产 映射的列数错误: 请帮帮我,我错过了什么 我的组合类型如下Spring Hibernate CompositeUserType映射的列数错误,spring,hibernate,Spring,Hibernate,我刚开始冬眠。写一个复合字体。当我运行代码时,我得到一个错误。 财产 映射的列数错误: 请帮帮我,我错过了什么 我的组合类型如下 public class EncryptedAsStringType implements CompositeUserType { @Override public String[] getPropertyNames() { return new String[] { "stockId", "stockCode", "stockName","stock
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());
}
}
我的域类股票有五个属性。(股票ID、股票代码、股票名称、状态、股票
(说明)
我需要将字段库存描述声明为复合字段类型
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;
}
}
当我尝试执行股票插入时。我在创建名为的bean时出错
在类路径资源[spring/config/./database/Hibernate.xml]中定义的“sessionFactory”:
调用init方法失败。嵌套异常为org.hibernate.MappingException:
属性映射的列数错误:com.stock.model.stock.stockDescription类型:
com.mycheck.EncryptedAsStringType
我哪里出错了?可以从代码示例和原始问题的注释中提取答案,但为了节省大家的阅读时间,我编写了一个快速摘要 如果声明一个将类型映射到n列的
CompositeUserType
,则除了@type
注释外,还必须在@columns
中声明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很高兴。您错过了@Column(name=“STOCK\u DESCRIPTION”)对吗?STOCK\u CC指的是描述。你看到有什么问题吗?@Column(name=“STOCK_CC”,unique=true,nullable=false,length=20)当我在用户类型类中的返回新字符串[]{“stockDescription”}中声明一个one属性时,代码是有效的。如果我添加了多个列,则返回新字符串[]{“stockDescription”,“stockName”};它说属性映射的列数不正确:因为您试图将3列拆分为1个类字段。将3列分隔为3个字段。这将解决部分问题Custom NamingStrategy应该能够为具有2列或更多列的CompositeUserType定义唯一的列名: