Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Hibernate无法保存实体之间的关系_Java_Hibernate_Many To Many - Fatal编程技术网

Java Hibernate无法保存实体之间的关系

Java Hibernate无法保存实体之间的关系,java,hibernate,many-to-many,Java,Hibernate,Many To Many,我已经创建了两个实体(学生和课程),具有多对多关系,我分别创建了学生和课程,这意味着我可以先创建没有课程的学生或没有学生的课程 <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hi

我已经创建了两个实体(学生和课程),具有多对多关系,我分别创建了学生和课程,这意味着我可以先创建没有课程的学生或没有学生的课程

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
    <class name="com.school.Student" table="student">
    <id name="id" column="student_id" length="32">
        <generator class="uuid"></generator>
    </id>
    <property name="fullName" type="string">
        <column name="full_name" length="40" />
    </property>
    <version name="version" column="VERSION" type="integer" />
    <property name="createdBy" type="string">
        <column name="created_by" length="50" />
    </property>
    <property name="createdDate" type="timestamp">
        <column name="created_date" length="29" />
    </property>
    <property name="updatedBy" type="string">
        <column name="updated_by" length="50" />
    </property>
    <property name="updatedDate" type="timestamp">
        <column name="updated_date" length="29" />
    </property>

    <set name="course" table="student_course"
        lazy="false" cascade="all" inverse="true">
        <key column="student_id" />
        <many-to-many column="course_id" class="com.school.Course" />
    </set>
        </class>


   <class name="com.school.Course" table="course">
    <id name="id" column="course_id" length="32">
        <generator class="uuid"></generator>
    </id>
    <property name="limit" type="int">
        <column name="limit" />
    </property>
    <property name="courseName" type="string">
        <column name="course_name" length="50" />
    </property>
    <version name="version" column="VERSION" type="integer" />
    <property name="createdBy" type="string">
        <column name="created_by" length="50" />
    </property>
    <property name="createdDate" type="timestamp">
        <column name="created_date" length="29" />
    </property>
    <property name="updatedBy" type="string">
        <column name="updated_by" length="50" />
    </property>
    <property name="updatedDate" type="timestamp">
        <column name="updated_date" length="29" />
    </property>

    <set name="student" table="student_course"
        lazy="false" cascade="all">
        <key>
            <column name="course_id"/>
        </key>
        <many-to-many column="student_id" class="com.school.Student" />
    </set>
</class>
</hibernate-mapping>
它没有为表student\u课程生成insert语句。 我是否需要为学生与课程的关系创建一个projo和dao,然后自己完成

我在网上读了一些例子,比如,我想我做的和他们差不多,但他们是关系双方的新对象

有人能指出我错过了什么吗?还是每次新学生创建或编辑课程集时,我都需要自己创建关系

非常感谢。

我们又来了

你的交往是双向的。在双向关联中,有一个所有者端(此处:
Course.student
,顺便说一句,应命名为
students
),还有一个反向端(此处:
student.Course
,顺便说一句,应命名为
courses

您的代码只设置关联的反向端,而不设置所有者端。因为Hibernate只考虑关联的所有者端,所以它不保存关联


因此,您需要调用
c.getStudents().add(student)

嗨,JB,谢谢,它真的保存了关系,现在就保存我;)
public class Student implements java.io.Serializable, Entity {

private static final long serialVersionUID = -546886879998950467L;
private String id;
private int version;
private String createdBy;
private String updatedBy;
private Date createdDate;
private Date updatedDate;
private String fullName;

private Set<Course> course = new HashSet<Course>(0);

public Set<Course> getCourse(){
    return course;
}

public void setCourse(Set<Course> c){
    this.course=c;
}

public boolean isNew() {
    return (getId() == null || getId().isEmpty());
}

public Student() {
}

public int getVersion() {
    return this.version;
}

public void setVersion(int version) {
    this.version = version;
}



@Override
public int hashCode() {
    return new BigInteger(getId(), 16).hashCode();
}

public boolean equals(Student s) {
    return getId().equals(s.getId());
}

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }

    if (obj instanceof Student) {
        Student s = (Student) obj;
        return equals(s);
    }

    return false;
}

public String getCreatedBy() {
    return createdBy;
}

public void setCreatedBy(String createdBy) {
    this.createdBy = createdBy;
}

public String getUpdatedBy() {
    return updatedBy;
}

public void setUpdatedBy(String updatedBy) {
    this.updatedBy = updatedBy;
}

public Date getCreatedDate() {
    return createdDate;
}

public void setCreatedDate(Date createdDate) {
    this.createdDate = createdDate;
}

public Date getUpdatedDate() {
    return updatedDate;
}

public void setUpdatedDate(Date updatedDate) {
    this.updatedDate = updatedDate;
}

public void setId(String id) {
    this.id = id;
}

public String getId() {
    return id;
}

public void setFullName(String fullName) {
    this.fullName = fullName;
}

public String getFullName() {
    return fullName;
}
public class Course implements java.io.Serializable, Entity {

private static final long serialVersionUID = -7827469634033428134L;
private String id;
private int version;
private String createdBy;
private String updatedBy;
private Date createdDate;
private Date updatedDate;
private String courseName;
private int limit;

private Set<Student> student = new HashSet<Student>(0);

public Set<Course> getStudent(){
    return student;
}

public void setStudent(Set<Student> s){
    this.student=s;
}

public boolean isNew() {
    return (getId() == null || getId().isEmpty());
}

public Course() {
}

public int getVersion() {
    return this.version;
}

public void setVersion(int version) {
    this.version = version;
}



@Override
public int hashCode() {
    return new BigInteger(getId(), 16).hashCode();
}

public boolean equals(Course c) {
    return getId().equals(c.getId());
}

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }

    if (obj instanceof Course) {
        Course c = (Course) obj;
        return equals(c);
    }

    return false;
}

public String getCreatedBy() {
    return createdBy;
}

public void setCreatedBy(String createdBy) {
    this.createdBy = createdBy;
}

public String getUpdatedBy() {
    return updatedBy;
}

public void setUpdatedBy(String updatedBy) {
    this.updatedBy = updatedBy;
}

public Date getCreatedDate() {
    return createdDate;
}

public void setCreatedDate(Date createdDate) {
    this.createdDate = createdDate;
}

public Date getUpdatedDate() {
    return updatedDate;
}

public void setUpdatedDate(Date updatedDate) {
    this.updatedDate = updatedDate;
}

public void setId(String id) {
    this.id = id;
}

public String getId() {
    return id;
}

public void setCourseName(String courseName) {
    this.courseName = courseName;
}

public String getCourseName() {
    return courseName;
}

public int getLimit() {
    return this.limit;
}

public void setLimit(int l) {
    this.limit = l;
}   
        for(int i=0;selectCourse.getChosenDataList()!=null && selectCourse.getChosenDataList().size()<i;i++){
        Course c = (Course)selectCourse.getChosenDataList()get(i);
        student.getCourseList().add(c);
    }

    .....
    ......


    try {
        getStudentService().saveOrUpdate(student);
    } catch (DataAccessException e) {
        MessageUtils.showErrorMessage(e.getMostSpecificCause().toString());
        return;
    }
Hibernate: 
insert 
into
    student
    (fullName, VERSION, created_by, created_date, updated_by, updated_date) 
values
    (?, ?, ?, ?, ?, ?, ?)