Hibernate CompositeUserType映射的列数错误 [英] Hibernate CompositeUserType mapping has wrong number of columns

查看:100
本文介绍了Hibernate CompositeUserType映射的列数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Hibernate的新手。编写一个CompositeUserType。当我运行代码时,我得到错误。
财产



映射的列数不正确:
请帮助我我缺少什么?



My CompositeUserType如下所示:

  public class EncryptedAsStringType implements CompositeUserType {
@Override
public String [] getPropertyNames(){
return new String [] {stockId,stockCode,stockName,stockDescription};


$ b @Override
public Type [] getPropertyTypes(){
// stockId,stockCode,stockName,modifiedDate
return new键入[] {
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)组件;

if(0 == property){
returnValue = auditData.getStockId();
} else if(1 == property){
returnValue = auditData.getStockCode();
} else if(2 == property){
returnValue = auditData.getStockName();
} return returnValue;

$ b @Override
public void setPropertyValue(final Object component,final int property,
final Object setValue)throws HibernateException {
final Stock auditData = (股票)组件;




$ b @Override
public Object nullSafeGet(final ResultSet resultSet,
final String [] names,
final SessionImplementor paramSessionImplementor,final Object paramObject)
抛出HibernateException,SQLException {
//这里的owner是类型为TestUser或者实际拥有的对象
Stock auditData = null;
final Integer createdBy = resultSet.getInt(names [0]);
//首次读取
后的延期检查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]));
}
返回auditData;


$ b @Override
public void nullSafeSet(final PreparedStatement preparedStatement,
final Object value,final int property,
final SessionImplementor sessionImplementor )
抛出HibernateException,SQLException {
if(null == value){

$ b} 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,状态,股票
描述)

我需要将字段股票描述声明为复合字段类型。

  private Integer stockId; 
私人字符串stockCode;
私人字符串stockName;
私人字符串状态;
private String stockDescription;

//构造函数


@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名称



'sessionFactory'在类路径资源[spring / config /../ database / Hibernate.xml]中定义: p>

调用init方法失败。嵌套异常是org.hibernate.MappingException:

属性映射的列数不正确:com.stock.model.Stock.stockDescription类型:

com.mycheck.EncryptedAsStringType



我在哪里错了?

解决方案

可以从代码示例中提取答案并将注释提取为原始问题,但为了节省大家的一些阅读时间,我编写了一个快速概要。



如果声明将类型映射到n列的 CompositeUserType ,则必须在 @Columns 中声明n列c $ c>除 @Type 注释外。示例:

  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很开心。


I am new to Hibernate. Writing a CompositeUserType. When I run the code I am getting error. property

mapping has wrong number of columns: Please help me what am I missing?

My CompositeUserType goes as follows

 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());


        }
    }

My Domain class Stock has five attributes. (stockId,stockCode,StockName,Status , Stock Description)

I need to declare the field Stock description as Composite field Type.

 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;
}

}

When I try to execute a insert for Stock. I am getting the error Error creating bean with name

'sessionFactory' defined in class path resource [spring/config/../database/Hibernate.xml]:

Invocation of init method failed. nested exception is org.hibernate.MappingException:

property mapping has wrong number of columns: com.stock.model.Stock.stockDescription type:

com.mycheck.EncryptedAsStringType

Where am I going wrong ?

解决方案

One can extract the answer from the code samples and the comments to the original question, but to save everyone some reading, I've compiled a quick summary.

If you declare a CompositeUserType that maps a type to n columns, you have to declare n columns in @Columns besides the @Type annotation. Example:

public class EncryptedAsStringType implements CompositeUserType {
    @Override
    public String[] getPropertyNames() {
       return new String[] { "stockId", "stockCode", "stockName","stockDescription" };
    }

    // ...
}

This CompositeUserType maps to 4 separate columns, therefore 4 separate @Column annotations have to be declared:

@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;
}

That's it and Hibernate is happy.

这篇关于Hibernate CompositeUserType映射的列数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆