我一直在尝试让 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)