java org.hibernate.AnnotationException:集合既没有泛型类型也没有 OneToMany.targetEntity()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6937987/
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
org.hibernate.AnnotationException: Collection has neither generic type or OneToMany.targetEntity()
提问by Jeremy S.
I used Hibernate Toolsto generate my Hibernate POJO mapping.
我使用Hibernate Tools来生成我的 Hibernate POJO 映射。
Unfortunately the code generated by Hibernate tools seems not to work, I get the exception
不幸的是,Hibernate 工具生成的代码似乎不起作用,我得到了异常
org.hibernate.AnnotationException: Collection has neither generic type or OneToMany.targetEntity()
org.hibernate.AnnotationException:集合既没有泛型类型也没有 OneToMany.targetEntity()
The code parts that generate the exception are
产生异常的代码部分是
/**
* ClassFlag generated by hbm2java
*/
@Entity
@Table(name = "class_flag", catalog = "incbszdb")
public class ClassFlag implements java.io.Serializable {
....
/* HERE */
private Set classFlagI18ns = new HashSet(0);
/* HERE */
public void setClassFlagI18ns(Set classFlagI18ns) {
this.classFlagI18ns = classFlagI18ns;
}
}
According to this post
根据这个帖子
and this post
和这篇文章
You have to change Hibernates generated code by yourself by hand.
您必须自己手动更改 Hibernate 生成的代码。
This is one thing I want to avoid. Any ideas what could be the problem?
这是我想要避免的一件事。任何想法可能是什么问题?
Regards
问候
JS
JS
回答by Jeremy S.
I have found a solution for me that works.
我找到了适合我的解决方案。
Simply check "Use Java 5 Syntax"as depicted in the attached Screenshot and Hibernate Tools generate the correct generic types for collections.
只需选中“使用 Java 5 语法”,如随附的屏幕截图所示,Hibernate 工具会为集合生成正确的泛型类型。
回答by Bozho
What the exceptions tells you is clear - yout @OneToMany
collection should either specify a concrete type (Set<AnotherEntity>
) or have @OneToMany(targetEntity=AnotherEntity.class)
异常告诉你的很清楚 - 你的@OneToMany
集合应该指定一个具体的类型 ( Set<AnotherEntity>
) 或者有@OneToMany(targetEntity=AnotherEntity.class)
回答by ChromeDome
Use Java 5 Syntax is the right answer... but it is very misleading. Should be Use Java 5+ Syntax.. who would think to use java 5.
使用 Java 5 语法是正确的答案......但它非常具有误导性。应该使用 Java 5+ 语法.. 谁会想到使用 java 5。
回答by levolutionniste
For those who needs. As far as I remember, with Java EE 5, the Java Enterprise Edition got a lot of functionnalities, particularly in the use of annotations and generics. So That's why I think checking "Use Java 5 Syntax" on Eclipse, or "Java 5 Compatibility" (not sure of the exact term on the UI) on Netbeans , when generating the Entities whith Hibernate, will make sure the generated code will take those 'new functionnalities' of Java 5 in count.
对于有需要的人。据我所知,在 Java EE 5 中,Java 企业版具有很多功能,特别是在注释和泛型的使用方面。所以这就是为什么我认为在 Eclipse 上检查“使用 Java 5 语法”或在 Netbeans 上检查“Java 5 兼容性”(不确定 UI 上的确切术语),在生成 Hibernate 的实体时,将确保生成的代码将Java 5 的那些“新功能”。
Because I have just 42 as reputation I'm unable to comment!!!
因为我的声誉只有 42,所以我无法发表评论!!!
回答by user3228876
Search on "Red Hat hbm2java docs" because the link is broken. Added bonus so you don't have to search. Select both of those check boxes to "modernize" the generated Java. Unfortunately, the labels of the check boxes do not indicate what happens if you check them. One would think that "Use Java 5 syntax" has many implications and since it is off by default that it is not the desired/typical behavior.
搜索“Red Hat hbm2java docs”,因为链接已损坏。添加了奖金,因此您无需搜索。选中这两个复选框以“现代化”生成的 Java。不幸的是,复选框的标签并不表示如果您选中它们会发生什么。人们会认为“使用 Java 5 语法”有很多含义,并且由于默认情况下它是关闭的,因此它不是所需的/典型的行为。
By all means check both boxes to use annotations and generics. Then tell Eclipse under "clean up" options to generate the serial version UID. Took me 3 days to figure all this out.
一定要选中这两个框以使用注释和泛型。然后告诉 Eclipse 在“清理”选项下生成串行版本 UID。我花了 3 天时间才弄清楚这一切。
回答by jmh
In order to use java 5 generics in an automated build, you can modify your arguments to enable jdk5 like this:
为了在自动构建中使用 java 5 泛型,您可以修改参数以启用 jdk5,如下所示:
<hbm2java jdk5="true">
This information is documented here: Red Hat hbm2java docs.
此信息记录在此处:Red Hat hbm2java docs。