0

我一直在尝试让 idbag 在休眠状态下工作。我一直在针对 MySql DB 和 HSQLDB 尝试它,并且我还尝试了几个不同版本的 Hibernate。Hibernate 4 和 Hibernate 3 给出的关于错误原因的信息略有不同,但主要都说同样的事情:Class Cast Exception。

我将我的所有代码都托管在一个公共 repo 中的 bitbucket 上,并且所有内容都包含用于创建带有相关表的 MySql 或 HSQL DB 的 DDL。我的 HSQLDB 版本是 2.2.9,MySql 版本是 5.1.66 - 我的偏好是让它在 MySql 上运行。

Git 克隆命令: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)
4

2 回答 2

0

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

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

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

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

于 2013-03-08T06:09:29.453 回答
0

我遇到了同样的错误,

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

  1. 你正在使用什么数据库加上
  2. 什么@GenericGenerator - 你正在使用什么策略。

我能够通过以下配置解决问题。

  1. 数据库 - MySQL
  2. 将策略更改为从原生增量

我的代码看起来像:@ElementCollection @JoinTable(name="USERS_ADDRESSES", joinColumns=@JoinColumn(name="USER_ID")) @GenericGenerator(name="hilogen",strategy="increment") @CollectionId(columns={@ Column(name="ADDR_ID")},generator="hilogen",type=@Type(type="long")) private List addressList = new ArrayList();

希望这有帮助。

谢谢,萨加尔维亚斯

于 2015-12-23T19:18:00.873 回答