Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: com.bean.Results.questions

赵小宾 发布于 2013/08/02 17:32
阅读 2K+
收藏 0
三个实体类: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;
	}
	

}

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;
	}
	

}
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;
}


}
测试代码:

package com.bean;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.classic.Session;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class SurveyTest {

	public static SessionFactory sf = null;

	@BeforeClass
	public static void BeforeClass() {
		sf = new AnnotationConfiguration().configure().buildSessionFactory();
		new SchemaExport(new AnnotationConfiguration().configure()).create(
				false, true);
	}

	@Test
	public void testSUrveysave() {
		Survey s = new Survey();
		SurveyType st = new SurveyType();
		st.setSt_des("教学反馈调查");
		s.setS_name("关于jsp课堂效果的调查");
		s.setS_type(st);

		Session session = sf.openSession();
		session.beginTransaction();

		session.save(s);
		// session.save(st);
		session.getTransaction().commit();
		session.close();
	}

	@Test
	public void testSurveyTypeSave() {
		Survey s = new Survey();
		SurveyType st1 = new SurveyType();
		SurveyType st2 = new SurveyType();
		SurveyType st3 = new SurveyType();
		SurveyType st4 = new SurveyType();
		SurveyType st5 = new SurveyType();
		SurveyType st6 = new SurveyType();
		st1.setSt_des("教学反馈调查类");
		st2.setSt_des("学生思想调查类");
		st3.setSt_des("教学研究类");
		st4.setSt_des("社会调查类");
		st5.setSt_des("网上投票类");
		st6.setSt_des("网上考试类");
		s.setS_name("关于jsp课堂效果的调查");
		s.setS_type(st1);

		Session session = sf.openSession();
		session.beginTransaction();

		session.save(st1);
		session.save(st2);
		session.save(st3);
		session.save(st4);
		session.save(st5);
		session.save(st6);
		session.save(s);
		session.getTransaction().commit();
		session.close();
	}

	@Test
	public void testSurveyDelete() {
		testSurveyTypeSave();
		SurveyType st = new SurveyType();

		Session session = sf.openSession();
		session.beginTransaction();
        st=(SurveyType)session.load(SurveyType.class, 1);
		session.delete(st);
		
		session.getTransaction().commit();
		session.close();
	}

	@AfterClass
	public static void afterClass() {
		sf.close();
	}

}
运行testSUrveysave(),报错信息如下:

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)

加载中
返回顶部
顶部