Illegal attempt to map a non collection as a @OneToMany,@ManyToMany or@Collect

问题描述

三个实体类Questions、survey和results。questions和Survey是多对多的关系,results是中间表。Questions类如下:package com.bean;import java.util.HashSet;import java.util.Set;import javax.persistence.CascadeType;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.ManyToMany;import javax.persistence.ManyToOne;@Entitypublic class Questions {private int q_id;//问题主键IDprivate Set<Survey> survey=new HashSet<Survey>();//外键,属于哪一个问卷private QuestionType q_type;//问题类型private AnswerSheet as;//答卷表private String q_head;//问题题目private String q_body;//问题主体private String q_key;//预留字段,如果是网上答卷,则是答案private String q_remarks;// 预留字段,备注private int q_number;//选项个数@Id@GeneratedValue public int getQ_id() {return q_id;}public void setQ_id(int q_id) {this.q_id = q_id;}@ManyToMany(mappedBy="question")public Set<Survey> getSurvey() {return survey;}public void setSurvey(Set<Survey> survey) {this.survey = survey;}public String getQ_head() {return q_head;}public void setQ_head(String q_head) {this.q_head = q_head;}public String getQ_body() {return q_body;}public void setQ_body(String q_body) {this.q_body = q_body;}public String getQ_key() {return q_key;}public void setQ_key(String q_key) {this.q_key = q_key;}public String getQ_remarks() {return q_remarks;}public void setQ_remarks(String q_remarks) {this.q_remarks = q_remarks;}@ManyToOne(cascade=CascadeType.ALL)@JoinColumn (name="qt_id")public QuestionType getQ_type() {return q_type;}public void setQ_type(QuestionType q_type) {this.q_type = q_type;}@ManyToOne@JoinColumn(name="as_id")public AnswerSheet getAs() {return as;}public void setAs(AnswerSheet as) {this.as = as;}@Column(nullable=true)public int getQ_number() {return q_number;}public void setQ_number(int q_number) {this.q_number = q_number;}}Survey类:package com.bean;import java.util.Date;import java.util.HashSet;import java.util.Set;import javax.persistence.CascadeType;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.JoinTable;import javax.persistence.ManyToMany;import javax.persistence.ManyToOne;import javax.persistence.OneToOne;//问卷实体:包括各种属性@Entitypublic class Survey {private int s_id;//问卷IDprivate String s_name;//问卷标题private String s_des;//问卷描述或者备注private boolean s_isOpen;//是否开放private SurveyType s_type;//问卷类型private int s_count;//问卷数量限制private Date s_createTime;//问卷创建时间private Date s_publishTime;//问卷发布时间private Date s_deadline;//问卷截止提交日期private String s_password;//问卷访问密码private String s_isGrade;//问卷是否是打分问卷private AnswerSheet as;//对应答卷private Results results;//对应结果表private Set<Questions> question= new HashSet<Questions>();//private User s_author;//问卷创建者@Id@GeneratedValuepublic int getS_id() {return s_id;}public void setS_id(int s_id) {this.s_id = s_id;}public String getS_name() {return s_name;}public void setS_name(String s_name) {this.s_name = s_name;}public String getS_des() {return s_des;}public void setS_des(String s_des) {this.s_des = s_des;}public boolean isS_isOpen() {return s_isOpen;}public void setS_isOpen(boolean s_isOpen) {this.s_isOpen = s_isOpen;}@ManyToOne@JoinColumn (name="st_id")public SurveyType getS_type() {return s_type;}public void setS_type(SurveyType s_type) {this.s_type = s_type;}public int getS_count() {return s_count;}public void setS_count(int s_count) {this.s_count = s_count;}public Date getS_createTime() {return s_createTime;}public void setS_createTime(Date s_createTime) {this.s_createTime = s_createTime;}public Date getS_publishTime() {return s_publishTime;}public void setS_publishTime(Date s_publishTime) {this.s_publishTime = s_publishTime;}public Date getS_deadline() {return s_deadline;}public void setS_deadline(Date s_deadline) {this.s_deadline = s_deadline;}public String getS_password() {return s_password;}public void setS_password(String s_password) {this.s_password = s_password;}@OneToOne(cascade = CascadeType.ALL,mappedBy="survey")//一对一双向关联public AnswerSheet getAs() {return as;}public void setAs(AnswerSheet as) {this.as = as;}@ManyToMany@JoinTable(name="results",joinColumns={@JoinColumn(name="s_id")},inverseJoinColumns={@JoinColumn(name="q_id")})public Set<Questions> getQuestion() {return question;}public void setQuestion(Set<Questions> question) {this.question = question;}@OneToOne(cascade = CascadeType.ALL,mappedBy="survey")//一对一双向关联public Results getResults() {return results;}public void setResults(Results results) {this.results = results;}public String getS_isGrade() {return s_isGrade;}public void setS_isGrade(String s_isGrade) {this.s_isGrade = s_isGrade;}}results:package com.bean;import java.util.Date;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.OneToMany;import javax.persistence.Table;@Entity@Table(name="results")public class Results {private int r_id;//统计结果表IDprivate Questions questions ;//对应问题题目,外键private Survey survey;//对应问卷,外键private Date r_creatDate;//统计结果表产生时间private int[] r_answerCount;//统计各种答案的数目@Id@GeneratedValuepublic int getR_id() {return r_id;}public void setR_id(int r_id) {this.r_id = r_id;}@OneToMany@JoinColumn(name="q_id")public Questions getQuestions() {return questions;}public void setQuestions(Questions questions) {this.questions = questions;}@OneToMany@JoinColumn (name="s_id")public Survey getSurvey() {return survey;}public void setSurvey(Survey survey) {this.survey = survey;}public Date getR_creatDate() {return r_creatDate;}public void setR_creatDate(Date r_creatDate) {this.r_creatDate = r_creatDate;}public int[] getR_answerCount() {return r_answerCount;}public void setR_answerCount(int[] r_answerCount) {this.r_answerCount = r_answerCount;}}出错信息:org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: com.bean.Results.questionsat org.hibernate.cfg.annotations.CollectionBinder.getCollectionBinder(CollectionBinder.java:266)at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1448)at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:754)at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:546)at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:291)at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1333)at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)at com.bean.SurveyTest.BeforeClass(SurveyTest.java:17)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)at java.lang.reflect.Method.invoke(Method.java:597)at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)at org.junit.runners.ParentRunner.run(ParentRunner.java:236)at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)java.lang.NullPointerExceptionat com.bean.SurveyTest.afterClass(SurveyTest.java:87)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)at java.lang.reflect.Method.invoke(Method.java:597)at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:37)at org.junit.runners.ParentRunner.run(ParentRunner.java:236)at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)大侠们,请赶快来看看吧!

解决方案

package com.bean;import java.util.Date;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.OneToMany;import javax.persistence.Table;@Entity@Table(name="results")public class Results {private int r_id;//统计结果表IDprivate Set<Questions> questions = new HashSet<Questions> ;//对应问题题目,外键private Survey survey;//对应问卷,外键private Date r_creatDate;//统计结果表产生时间private int[] r_answerCount;//统计各种答案的数目@Id@GeneratedValuepublic int getR_id() {return r_id;}public void setR_id(int r_id) {this.r_id = r_id;}@OneToMany@JoinColumn(name="q_id")public Set<Questions> getQuestions() {return questions;}public void setQuestions(Set<Questions> questions) {this.questions = questions;}@OneToMany@JoinColumn (name="s_id")public Survey getSurvey() {return survey;}public void setSurvey(Survey survey) {this.survey = survey;}public Date getR_creatDate() {return r_creatDate;}public void setR_creatDate(Date r_creatDate) {this.r_creatDate = r_creatDate;}public int[] getR_answerCount() {return r_answerCount;}public void setR_answerCount(int[] r_answerCount) {this.r_answerCount = r_answerCount;}}

时间: 2024-09-21 18:43:31

Illegal attempt to map a non collection as a @OneToMany,@ManyToMany or@Collect的相关文章

session报错Illegal attempt to associate a collection with two open sessions

问题描述 Illegalattempttoassociateacollectionwithtwoopensessionsatorg.hibernate.collection.AbstractPersistentCollection.setCurrentSession(AbstractPersistentCollection.java:410)atorg.hibernate.event.def.OnUpdateVisitor.processCollection(OnUpdateVisitor.ja

在java中如何获取或初始化scala.collection.immutable.Map?

问题描述 在java中如何获取或初始化scala.collection.immutable.Map? java调用kafka API时需要一个一个scala.collection.immutable.Map参数,但不知道如何初始化或通过其他API获取 解决方案 java.util.Map javaMap = new java.util.HashMap();scala.collection.immutable.Map scalaImmutableMap = scala.collection.Jav

为什么 map不继承与collection collection不是所有集合的父类吗

问题描述 为什么 map不继承与collection collection不是所有集合的父类吗 为什么 map不继承与collection collection不是所有集合的父类吗 解决方案 map和collection的数据结构是不一样的collection定义了其实现类的基本操作,map也定义了其实现类的基本操作,操作不同,所以接口不同,而这正是因为接口是种规范. 解决方案二: Collection接口 Collection是最基本的集合接口,一个Collection代表一组Object,即

Java基础-18总结Map,HashMap,HashMap与Hashtable区别,Collections工具类

你需要的是什么,直接评论留言. 获取更多资源加微信公众号"Java帮帮" (是公众号,不是微信好友哦) 还有"Java帮帮"今日头条号,技术文章与新闻,每日更新,欢迎阅读 学习交流请加Java帮帮交流QQ群553841695 分享是一种美德,分享更快乐! 1:Map(掌握) (1)将键映射到值的对象.一个映射不能包含重复的键:每个键最多只能映射到一个值.  (2)Map和Collection的区别? A:Map 存储的是键值对形式的元素,键唯一,值可以重复.夫妻对

java.容器类(list,queue,map,set)

1.Collection public interface Collection<E> extendsIterable<E>  Set,Queue和List 都继承了Collection.Map没有. Collection接口的方法:     boolean add(Object o)   :向集合中加入一个对象的引用     void clear()                        :删除集合中所有的对象,即不再持有这些对象的引用     boolean isEmp

JAVA Map基礎問題

问题描述 map.keySet().removeAll(map.keySet());自定義了一個Map,但是這一行執行沒有得到想要的結果.removeALL方法是set的,沒有重載.代碼如下:package c11;//: c11:Map1.java// Things you can do with Maps.// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002// www.BruceEckel.com. See copyright n

浅谈Java中常用数据结构的实现类 Collection和Map_java

线性表,链表,哈希表是常用的数据结构,在进行Java开发时,JDK已经为我们提供了一系列相应的类来实现基本的数据结构.这些类均在java.util包中.本文试图通过简单的描述,向读者阐述各个类的作用以及如何正确使用这些类. Collection ├List │├LinkedList │├ArrayList │└Vector │ └Stack └Set Map ├Hashtable ├HashMap └WeakHashMap Collection接口 Collection是最基本的集合接口,一个C

Rails Routes中new、collection、member的区别浅析_ruby专题

RESTful风格的路由动词默认有7个(分别为:index, show, create, new, edit, update, destroy).有时我们需要自定义路由,这时就要用到:on参数.:on参数有三种取值,分别为collection,member,new.  如果想添加一个member方式的路由,可以这样: 复制代码 代码如下: resources :photos do   member do     get 'preview'   end end 将会添加一个路由:GET请求/pho

Java集合框架List,Map,Set等全面介绍

Java Collections Framework是Java提供的对集合进行定义,操作,和管理的包含一组接口,类的体系结构.   Java集合框架的基本接口/类层次结构: java.util.Collection [I]+--java.util.List [I]   +--java.util.ArrayList [C]   +--java.util.LinkedList [C]   +--java.util.Vector [C]      +--java.util.Stack [C]+--ja