idBag生成器类强制转换异常

时间:2013-03-07 05:13:20

标签: mysql hibernate-mapping hsqldb

我一直试图让一个idbag在hibernate中工作。我一直在尝试对抗MySql DB和HSQLDB,我也试过了几个不同版本的Hibernate。 Hibernate 4和Hibernate 3提供的错误原因略有不同,但两者在主要内容中表示相同:Class Cast Exception。

我在公共仓库中托管了bitbucket上的所有代码,所有内容都包括用于创建带有相关表的MySql或HSQL DB的DDL。我的HSQLDB版本是2.2.9,MySql版本是5.1.66 - 我的偏好是让它在MySql上工作。

Git Clone命令: git clone https://pphi@bitbucket.org/pphi/idbag.git

网页: https://pphi@bitbucket.org/pphi/idbag.git

使用Hibernate 4.0.1时,我得到了这个堆栈跟踪:

    Hibernate: insert into idBagTest.Team (nickname, mascot) values (?, ?)
Hibernate: insert into idBagTest.Famous_Fan (first_name, last_name) values (?, ?)
Hibernate: insert into idBagTest.famous_fan_team (Team_id, team_fan_id, Famous_Fan_id ) values (?, ?, ?)
Exception in thread "main" java.lang.ClassCastException: org.hibernate.id.IdentifierGeneratorHelper$2 cannot be cast to java.lang.Long
    at org.hibernate.type.descriptor.java.LongTypeDescriptor.unwrap(LongTypeDescriptor.java:36)
    at org.hibernate.type.descriptor.sql.BigIntTypeDescriptor$1.doBind(BigIntTypeDescriptor.java:57)
    at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:92)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:280)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:275)
    at org.hibernate.persister.collection.AbstractCollectionPersister.writeIdentifier(AbstractCollectionPersister.java:919)
    at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1252)
    at org.hibernate.action.internal.CollectionRecreateAction.execute(CollectionRecreateAction.java:58)
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:354)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:279)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1213)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:402)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
    at com.intertech.dao.TeamDaoHibernateImpl.saveTeam(TeamDaoHibernateImpl.java:19)
    at com.intertech.Main.main(Main.java:22)

使用Hibernate 3.1时,我得到的堆栈跟踪略有不同:

Hibernate: insert into Team (id, nickname, mascot) values (null, ?, ?)
Hibernate: insert into Famous_Fan (id, first_name, last_name) values (null, ?, ?)
Hibernate: insert into famous_fan_team (Team_id, team_fan_id, Famous_Fan_id ) values (?, ?, ?)
5120 [main] INFO org.hibernate.type.LongType - could not bind value 'POST_INSERT_INDICATOR' to parameter: 2; org.hibernate.id.IdentifierGeneratorFactory$2 cannot be cast to java.lang.Long
Exception in thread "main" java.lang.ClassCastException: org.hibernate.id.IdentifierGeneratorFactory$2 cannot be cast to java.lang.Long
    at org.hibernate.type.LongType.set(LongType.java:65)
    at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:154)
    at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:136)
    at org.hibernate.persister.collection.AbstractCollectionPersister.writeIdentifier(AbstractCollectionPersister.java:829)
    at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1160)
    at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:58)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:171)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
    at com.intertech.dao.TeamDaoHibernateImpl.saveTeam(TeamDaoHibernateImpl.java:19)
    at com.intertech.Main.main(Main.java:22)

2 个答案:

答案 0 :(得分:0)

https://hibernate.onjira.com/browse/HHH-397

使用MySql和Hibernate时,显然不支持在idBag中使用identity作为生成器类。用增量替换了生成器类,得到了这个:

Hibernate:插入idBagTest.Team(昵称,吉祥物)值(?,?) Hibernate:插入idBagTest.Famous_Fan(first_name,last_name)值(?,?) Hibernate:从idBagTest.Famous_Fan_Team中选择max(team_fan_id) Hibernate:插入idBagTest.Famous_Fan_Team(Team_id,team_fan_id,Famous_Fan_id)值(?,?,?)

已使用工作代码更新存储库。

答案 1 :(得分:0)

我遇到了同样的错误,

经过一番研究后,我得出结论,这取决于两件事。

  1. 您正在使用的数据库加上
  2. @GenericGenerator是什么 - 你正在使用什么策略。
  3. 我能够通过以下配置解决问题。

    1. DB - MySQL
    2. 将策略更改为从原生
    3. 增加

      我的代码如下:     @ElementCollection     @JoinTable(name =" USERS_ADDRESSES",joinColumns = @ JoinColumn(name =" USER_ID"))     @GenericGenerator(名称=" hilogen",策略="增量&#34)     @CollectionId(列= {@柱(名称=" ADDR_ID")},发电机=" hilogen",类型= @类型(类型="长&#34))     private List addressList = new ArrayList();

      希望得到这个帮助。

      感谢, Sagar Vyas