Java 休眠@CollectionId 有错误:HHH000346:托管刷新期间出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32595527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate @CollectionId has ERROR: HHH000346: Error during managed flush
提问by amin
I write some of this code in hibernate .In this code I want to insert a collection into database as new table , this code create table and mapped foreign key
but couldn't insert data in table and throw exception .
this is my hibernate code :
我在 hibernate 中编写了一些代码。在这段代码中,我想将一个集合作为新表插入到数据库中,这段代码创建表并映射foreign key
但无法在表中插入数据并抛出异常。这是我的休眠代码:
@ElementCollection()
@JoinTable(name="user_address",
joinColumns=@JoinColumn(name="userId")
)
@GenericGenerator(name = "hilo-gen", strategy ="hilo")
@CollectionId(columns = { @Column(name="address_id") }, generator ="hilo-gen", type =@Type(type="long"))
and this is some error :
这是一些错误:
org.hibernate.internal.SessionImpl mapManagedFlushFailure
ERROR: HHH000346: Error during managed flush [org.hibernate.id.IdentifierGeneratorHelper cannot be cast to java.lang.Long]
Exception in thread "main" java.lang.ClassCastException: org.hibernate.id.IdentifierGeneratorHelper cannot be cast to java.lang.Long
采纳答案by Tomasz Bartnik
Try to use different generator strategy.@GenericGenerator(name = "inc-gen", strategy ="increment")
should work fine.
尝试使用不同的生成器策略。@GenericGenerator(name = "inc-gen", strategy ="increment")
应该工作正常。
Read more -> 5.1.2.2.1. Various additional generators.
阅读更多 -> 5.1.2.2.1。各种额外的发电机。
回答by user11676409
Add this file in hibernate.cfg.xml false
在 hibernate.cfg.xml 中添加此文件 false
in database execute below script SET FOREIGN_KEY_CHECKS = 0; SET GLOBAL FOREIGN_KEY_CHECKS=0;
在数据库中执行下面的脚本 SET FOREIGN_KEY_CHECKS = 0; 设置全局 FOREIGN_KEY_CHECKS=0;