首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web开发 >

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

2013-08-09 
Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @Collect三个实体类Questions

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;

@Entity
public class Questions {
private int q_id;//问题主键ID
private 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;


//问卷实体:包括各种属性
@Entity
public class Survey {
private int s_id;//问卷ID
private 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
@GeneratedValue
public 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;//统计结果表ID

private Questions questions ;//对应问题题目,外键
private Survey survey;//对应问卷,外键
private Date r_creatDate;//统计结果表产生时间
private int[] r_answerCount;//统计各种答案的数目
@Id
@GeneratedValue
public 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.questions
at 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.NullPointerException
at 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)


大侠们,在线等待啊! Hibernate?Annotation JAVA
[解决办法]
com.bean.SurveyTest.afterClass(SurveyTest.java:87)
这是什么?空指针异常了
[解决办法]
results类对question和Survey应该写@ManytoOne,就可以啦!

热点排行