休眠中的映射列表<CustomClass> - ClassCastException
Posted
技术标签:
【中文标题】休眠中的映射列表<CustomClass> - ClassCastException【英文标题】:Mapping List<CustomClass> in hibernate - ClassCastException 【发布时间】:2012-07-15 05:50:16 【问题描述】:我在休眠中映射列表时遇到困难。我想知道你对我的类、映射和数据库方案的看法。如果一切正常,我将不胜感激抛出异常的一些帮助。提前致谢!
我有以下课程:
public class AuditEntry
private long id;
private String typeLigne;
private String famille;
private String user;
private String libelle ;
private String processName;
private String caseId;
private Date dateCreation = null;
private List<AuditEntryStringMap> libelles;
/* Getters and setters generated automatically... */
public class AuditEntryStringMap
private long id ;
private String key ;
private String value ;
public AuditEntryStringMap(String key, String value)
this.key = key ;
this.value = value ;
/* Getters and setters generated automatically... */
我的 hbm 文件如下:
<class name="AuditEntryStringMap" table="AUDIT_I18N">
<cache usage="read-write" include="all" />
<id name="id" type="long">
<column name="AUDIT_I18N_ID"></column>
<generator class="sequence" >
<param name="sequence">AUDIT_I18N_SEQ</param>
</generator>
</id>
<property name="key" column="KEY_" />
<property name="value" column="VALUE_"/>
</class>
<class name="AuditEntry" table="AUDIT">
<cache usage="read-write" include="all" />
<id name="id" type="long">
<column name="AUDIT_ID"></column>
<generator class="sequence" >
<param name="sequence">AUDIT_SEQ</param>
</generator>
</id>
<property name="typeLigne" column="TYPELIGNE" />
<property name="libelle" column="LIBELLE" />
<property name="user" column="USERNAME" />
<property name="processName" column="PROCESSNAME" />
<property name="caseId" column="CASE_ID" />
<property name="dateCreation" insert="false" update="false">
<column name="DCRE" sql-type="date" default="sysdate"></column>
</property>
<list name="libelles" table="AUDIT_I18N">
<key>
<column name="FK_AUDIT_ID" sql-type="number(19,0)" />
</key>
<index type="string" column="KEY_" />
<element type="string" column="VALUE_" />
</list>
</class>
最后,我的sql脚本如下:
create sequence AUDIT_SEQ;
create sequence AUDIT_I18N_SEQ;
create table AUDIT (
AUDIT_ID number(19,0) not null,
TYPELIGNE varchar2(255 char),
LIBELLE varchar2(255 char),
USERNAME varchar2(255 char),
CASE_ID varchar2(255 char),
PROCESSNAME varchar2(255 char),
DCRE date default sysdate,
primary key (AUDIT_ID));
create table AUDIT_I18N (
AUDIT_I18N_ID number(19,0) not null,
KEY_ varchar2(255 char),
VALUE_ varchar2(255 char),
FK_AUDIT_ID number(19, 0),
primary key (AUDIT_I18N_ID))
ALTER TABLE AUDIT_I18N add constraint FK_AUDIT_I18N foreign key (FK_AUDIT_ID) references AUDIT;
Hibernate 在提交时抛出异常之前会生成以下查询:
Hibernate: select AUDIT_SEQ.nextval from dual
Hibernate: insert into AUDIT (TYPELIGNE, LIBELLE, USERNAME, PROCESSNAME, CASE_ID, AUDIT_ID) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into AUDIT_I18N (FK_AUDIT_ID, KEY_, VALUE_) values (?, ?, ?)
例外:
com.bpm.domain.AuditEntryStringMap cannot be cast to java.lang.String
当调用 GenericDaoHibernate.save(T entity) 时会发生这种情况。
【问题讨论】:
【参考方案1】:解决了使用集合而不是列表的问题...这在映射中:
<set name="libelles" lazy="extra" cascade="all-delete-orphan">
<cache usage="read-write" include="all" />
<key column="FK_AUDIT_ID"/>
<one-to-many class="AuditEntryStringMap" />
</set>
另外,在 hibernate.cfg.xml 中没有引用我用于 AuditEntryStringMap 的 hbm。但仅此一项并不能解决列表标签的问题。
半个分辨率所以...如果有人知道它为什么不适用于列表,将不胜感激。
【讨论】:
以上是关于休眠中的映射列表<CustomClass> - ClassCastException的主要内容,如果未能解决你的问题,请参考以下文章