当前位置:编程学习 > JAVA >>

Hibernate使用JPA注解方式表示多对多关系

我按照网上的方式配置的,但是就是不好使。 
代码如下: 

package org.zmx.hib;

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.ManyToMany;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

/**
 * Teacher entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name="TEACHER") 
public class Teacher implements java.io.Serializable {

// Fields
@Id
@GeneratedValue(generator = "ud")
@GenericGenerator(name = "ud", strategy="sequence",parameters = {@Parameter(name="sequence",value="SEQ_ALL")})
private Long tid;
private String username;
@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, mappedBy = "teachers", targetEntity = org.zmx.hib.Student.class) 
private Set<Student> students = new HashSet<Student>();

// Constructors

public Set getStudents() {
return students;
}

public void setStudents(Set students) {
this.students = students;
}

/** default constructor */
public Teacher() {
}

/** full constructor */
public Teacher(String username) {
this.username = username;
}

// Property accessors

public Long getTid() {
return this.tid;
}

public void setTid(Long tid) {
this.tid = tid;
}

public String getUsername() {
return this.username;
}

public void setUsername(String username) {
this.username = username;
}

}





package org.zmx.hib;

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.Table;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;


@Entity
@Table(name="STUDENT")  
public class Student implements java.io.Serializable {

// Fields
@Id
@GeneratedValue(generator = "ud")
@GenericGenerator(name = "ud", strategy="sequence",parameters = {@Parameter(name="sequence",value="SEQ_ALL")})
private Long sid;
private String username;

 @ManyToMany(cascade={CascadeType.MERGE,CascadeType.PERSIST}  
     )  
     @JoinTable(name="ST",  
                joinColumns={@JoinColumn(name="sid")},  
                inverseJoinColumns={@JoinColumn(name="tid")}  
     )  
private Set<Teacher> teachers = new HashSet<Teacher>();

// Constructors

public Set getTeachers() {
return teachers;
}

public void setTeachers(Set teachers) {
this.teachers = teachers;
}

/** default constructor */
public Student() {
}

/** full constructor */
public Student(String username) {
this.username = username;
}

// Property accessors

public Long getSid() {
return this.sid;
}

public void setSid(Long sid) {
this.sid = sid;
}

public String getUsername() {
return this.username;
}

public void setUsername(String username) {
this.username = username;
}

}






package org.zmx.action;

import org.zmx.hib.Student;
import org.zmx.hib.StudentDAO;
import org.zmx.hib.Teacher;

import com.opensymphony.xwork2.ActionSupport;


public class TestAction extends ActionSupport {

/**
 * 保存方法
 */
public String save(){
s1.getTeachers().add(t1);
s1.getTeachers().add(t2);
s2.getTeachers().add(t1);
s2.getTeachers().add(t2);
sdao.save(s1);
sdao.save(s2);
return "save";
}
private Student s1;
private Student s2;
private Teacher t1;
private Teacher t2;
private StudentDAO sdao=new StudentDAO();
public Student getS1() {
return s1;
}
public void setS1(Student s1) {
this.s1 = s1;
}
public Student getS2() {
return s2;
}
public void setS2(Student s2) {
this.s2 = s2;
}
public Teacher getT1() {
return t1;
}
public void setT1(Teacher t1) {
this.t1 = t1;
}
public Teacher getT2() {
return t2;
}
public void setT2(Teacher t2) {
this.t2 = t2;
}

}





org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: org.zmx.hib.Teacher
org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:219)
org.hibernate.type.EntityType.getIdentifier(EntityType.java:397)
org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:78)
org.hibernate.persister.collection.AbstractCollectionPersister.writeElement(AbstractCollectionPersister.java:755)
org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1143)
org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:26)
org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:145)
org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)



补充: 
这段代码是我由原来的配置文件的形式修改成注解的,但是修改完了就不好使了。 

如果只保存主表就会报错,如果先保存从表,再保存主表,就不会有问题。 

而且如果把:cascade = { CascadeType.PERSIST, CascadeType.MERGE } 修改成cascade = { CascadeType.All}
 也好使。不知道为什么??? 

我看网上的Demo都是这么写的啊 为什么我的不好使??? 

什么双向关联和单向关联什么的,我这个貌似现在单向关联就不好使啊。。。。。
--------------------编程问答-------------------- 为啥没人关注我的问题呢??? --------------------编程问答-------------------- 纠结啊 求助啊啊!!!我都弄了两天了。。。。。
补充:Java ,  Web 开发
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,