在进行查询时出现的错误。
Criteria crit = session.createCriteria(getPersistentClass());
for (Criterion c : criterion) {
crit.add(c);
}
return crit.list();
}
出现的主要异常如下:
org.hibernate.exception.GenericJDBCException: could not initialize a collection: ×××.×××.caseVectorDetail#0000000B]
[color=red]at org.hibernate.exception.[b]SQLStateConverter[/b].handledNonSpecificException[/color]
........................
[color=red]Caused by: java.sql.SQLException: Invalid value for getInt() - '0000002A' in column 2[/color]
at com.mysql.jdbc.ResultSet.getIntFromString(ResultSet.java:2573)
at com.mysql.jdbc.ResultSet.getNativeInt(ResultSet.java:7722)
[color=red]比较怪异的是,如果我把CaseVecetor中的 多的一端由List改为Set,就一切正常,不知道为啥,急盼知道原因!!!!![/color]
类和相关的映射文件如下:
public class CaseVector implements java.io.Serializable {
private String caseVectorId;
private String parameter;
private Date trainDate;
private boolean validate;
private List caseVectorDetail;
..................
public class CaseVectorDetail {
private String caseVectorDetailId;
private CaseVector caseVector;
private String svmVector;
........................
<hibernate-mapping
<class name="com.oneloong.sddes.biz.reason.vo.CaseVector" table="CaseVector" dynamic-update="true" dynamic-insert="true" >
<id
name="caseVectorId"
column="caseVectorId"
type="java.lang.String"
length="32"
>
<generator class="com.oneloong.sddes.biz.utils.IncrementGenerator">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-CaseVector.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<list
name="caseVectorDetail"
table="CaseVectorDetail"
lazy="false"
inverse="true"
cascade="none"
>
<key
column="caseVectorId"
>
</key>
<index
column="caseVectorDetailId"
/>
<one-to-many
class="com.oneloong.sddes.biz.reason.vo.CaseVectorDetail"
/>
</list>
<property
name="trainDate"
type="date"
column="trainDate"
/>
<property
name="validate"
type="boolean"
column="validate"
/>
<property
name="parameter"
type="string"
column="parameter"
length="2000"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-CaseVector.xml
containing the additional properties and place it in your merge dir.
-->
</class>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
<class name="com.oneloong.sddes.biz.reason.vo.CaseVectorDetail" table="CaseVectorDetail" dynamic-update="true" dynamic-insert="true" >
<id
name="caseVectorDetailId"
column="caseVectorDetailId"
type="java.lang.String"
length="32"
>
<generator class="com.oneloong.sddes.biz.utils.IncrementGenerator">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-CaseVectorDetail.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<many-to-one
name="caseVector"
class="com.oneloong.sddes.biz.reason.vo.CaseVector"
cascade="none"
outer-join="auto"
column="caseVectorId"
/>
<property
name="svmVector"
type="string"
column="svmVector"
length="2000"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-CaseVectorDetail.xml
containing the additional properties and place it in your merge dir.
-->
</class>
由于把主键设置为string,当用hibernate处理后,会自己把主键当作Integer来处理,所以主键最好不要用string,否则会出现Invalid value for getInt() -的错误!
原因:
索引集合类(Indexed collections)
所有的集合映射,除了set和bag语义的以外,都需要指定一个集合表的索引字段(index column)——用于对应到数组索引,或者List的索引,或者Map的关键字。通过<map-key>,Map 的索引可以是任何基础类型;若通过<map-key-many-to-many>,它也可以是一个实体引用;若通过<composite-map-key>,它还可以是一个组合类型。数组或列表的索引必须是integer类型,并且使用 <list-index>元素定义映射。被映射的字段包含有顺序排列的整数(默认从0开始)。