关闭

hibernate在做一对多关联时,保存一方的对象,报出不能插入外键的错误的解决方法

标签: hibernatestringgeneratorexceptionsessionclass
2951人阅读 评论(0) 收藏 举报
分类:
写这篇文章将自己的解决方法保存,好记性,不如烂博客,哈哈。我写了两个类Teacher、Student,其中Teacher是一方,Student是多方,它们的关系通过在student的表中的外键Tech_ID来维护的,要做的功能是在保存一方的同时能保存多方。先贴出正确的代码:
@Entity
@Table(name="TEACHERS")
@SequenceGenerator(name="SEQ_TEACHERS",sequenceName="SEQ_TEACHERS",allocationSize=1,initialValue=1)
public class Teacher {

	
	private Long id;
	
	private String code;
	
	private String name;
	
	private String type;
	
	private Set<Student> students;
	
	@Id
	@GeneratedValue(generator="SEQ_TEACHERS",strategy=GenerationType.SEQUENCE)
	@Column(name="id")
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	
	@Column(name="CODE")
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	
	@Column(name="NAME")
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	@Column(name="TYPE")
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	
	@OneToMany(fetch=FetchType.EAGER,mappedBy="teacher",cascade=CascadeType.ALL)
//	@Cascade(value={CascadeType.SAVE_UPDATE})
//	@JoinColumn(name="TECH_ID")
	public Set<Student> getStudents() {
		return students;
	}
	public void setStudents(Set<Student> students) {
		this.students = students;
	}
	
	
	
}

@Entity
@Table(name="STUDENTS")
@SequenceGenerator(name="SEQ_STUDENTS",sequenceName="SEQ_STUDENTS",allocationSize=1,initialValue=1)
public class Student {

	
	private Long id;
	
	private String code;
	
	private String name;
//	@Column(name="TECH_ID")
//	private Long techId;
	
	private Teacher teacher;
	
	@Id
	@GeneratedValue(generator="SEQ_STUDENTS",strategy=GenerationType.SEQUENCE)
	@Column(name="id")
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	
	@Column(name="CODE")
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	
	@Column(name="NAME")
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
//	public Long getTechId() {
//		return techId;
//	}
//	public void setTechId(Long techId) {
//		this.techId = techId;
//	}
	
	@ManyToOne
	@JoinColumn(name="TECH_ID")
	public Teacher getTeacher() {
		return teacher;
	}
	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}
}

public class AssociationsTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
//		SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
		SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
	    Session session = sessionFactory.openSession();
	    Transaction trans = session.beginTransaction();
	    Teacher t = new Teacher();
	    t.setCode("000001");
	    t.setName("xiaoxue");
	    t.setType("shuxue");
	    Set<Student> list = new HashSet<Student>();
	    Student s0 = new Student();
	    s0.setCode("000001");
	    s0.setName("xioali");
	    s0.setTeacher(t);
	    list.add(s0);
	    Student s1 = new Student();
	    s1.setCode("000002");
	    s1.setName("zhangsan");
	    s1.setTeacher(t);
	    list.add(s1);
	    t.setStudents(list);
	    session.save(t);
	    trans.commit();

	}

}


在写这个功能的时候遇到了不少的问题,真的是不写不知道,写了才知道问题多多啊,先将自己的测试结果贴出:

之前我在测试的时候没有把student的属性teacher赋值,加了以后就没有问题
测试:
1、当我把Cascade从java.persistence转换成hibernate特有的,这样没有影响

2、我将@OneToMany中的mappedby去掉以后,就有问题,报出teachers_students中间表不存在,这个问题一个是因为我的配置文件没有配置自动建表,貌似hibernate默认是通过建中间表来实现一对多的关联关系。

3、在一方(Teacher)中的student集合上的JoinColumn(name='tech_id'),同时将多方(Student)的teacher属性上的注解多去掉,则出现下面的错误:
Exception in thread "main" org.hibernate.MappingException: Could not determine type for: com.myHibernate.vo.Teacher, for columns: [org.hibernate.mapping.Column(teacher)]
 at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:266)
 at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:253)


 at org.hibernate.mapping.Property.isValid(Property.java:185)
 at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:440)
 at org.hibernate.mapping.RootClass.validate(RootClass.java:192)
 at org.hibernate.cfg.Configuration.validate(Configuration.java:1102)
 at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1287)
 at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
 at com.myHibernate.test.AssociationsTest.main(AssociationsTest.java:21)

 

还可以看我的帖子了解情况:hibernate 一对多 帖子

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:24348次
    • 积分:437
    • 等级:
    • 排名:千里之外
    • 原创:19篇
    • 转载:9篇
    • 译文:0篇
    • 评论:5条
    最新评论