如何使用hibernate和spring在双向的多对多关系中基于子表数据从父表数据中获取数据

时间:2013-08-24 06:32:27

标签: spring-mvc

我在oracle数据库中的多对多关系中有两个如下表。

学生表
..................
ID(主键)
FIRSTNAME
LASTNAME
EMAIL
电话

课程表

列名
............
ID(主键)
ROOM
NAME

这是我拥有的两个表,中间表就像这样

Student_Course
..............
列名
..........
STUDENT_ID(来自学生表的主键和外键)
COURSE_ID(课程表中的主键和外键)

我有两个实体类

Student.java
.........

import javax.persistence.*;




import java.util.*;




@Entity


@Table(name = "student")


public class Student {


@Id


@GeneratedValue


private Integer id;




// @ManyToMany(fetch=FetchType.LAZY,cascade =
// CascadeType.ALL,targetEntity=Course.class)
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "student_course", joinColumns = @JoinColumn(name = "student_id"),          inverseJoinColumns = @JoinColumn(name = "course_id"))
private List<Course> follows;

@Column
private String firstname;

@Column
private String lastname;

@Column
private String email;

@Column
private String phone;

public Integer getId() {
    return id;
}

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

public List<Course> getFollows() {
    return follows;
}

public void setFollows(List<Course> follows) {
    this.follows = follows;
}

public String getFirstname() {
    return firstname;
}

public void setFirstname(String firstname) {
    this.firstname = firstname;
}

public String getLastname() {
    return lastname;
}

public void setLastname(String lastname) {
    this.lastname = lastname;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

}


Course.java ............

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
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.Table;

import java.util.*;

@Entity
@Table(name = "course")
public class Course {

@Id
@GeneratedValue
private Integer id;

// @ManyToMany(fetch=FetchType.LAZY,cascade =
// CascadeType.ALL,targetEntity=Student.class)
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "student_course", joinColumns = @JoinColumn(name = "course_id"), inverseJoinColumns = @JoinColumn(name = "student_id"))
private List<Student> followedBy;

private String room;

private String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Integer getId() {
    return id;
}

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

public List<Student> getFollowedBy() {
    return followedBy;
}

public void setFollowedBy(List<Student> followedBy) {
    this.followedBy = followedBy;
}

public String getRoom() {
    return room;
}

public void setRoom(String room) {
    this.room = room;
}

}

这里关系正常...

students.jsp
....................................
在此页面中显示学生数据以及两个链接,一个用于向学生添加课程,另一个用于删除学生 ..........................................

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<h2>Users Manager</h2>

<form:form method="post" action="addstudent.html" commandName="student">

<table>
<tr>
    <td><form:label path="firstname"><spring:message code="label.firstname"/>         </form:label></td>
    <td><form:input path="firstname" /></td>
</tr>
<tr>
    <td><form:label path="lastname"><spring:message code="label.lastname"/></form:label></td>
    <td><form:input path="lastname" /></td>
</tr>
<tr>
    <td><form:label path="email"><spring:message code="label.email"/></form:label></td>
    <td><form:input path="email" /></td>
</tr>
<tr>
    <td><form:label path="phone"><spring:message code="label.telephone"/></form:label></td>
    <td><form:input path="phone" /></td>
</tr>
<tr>
    <td colspan="2">
        <input type="submit" value="<spring:message code="label.add"/>"/>
    </td>
</tr>
</table>
</form:form>
<h3>Users</h3>
<c:if  test="${!empty studentsList}">
<table>
<tr>
<th><spring:message code="label.lastname"/></th>
<th><spring:message code="label.firstname"/></th>
<th><spring:message code="label.email"/></th>
<th><spring:message code="label.telephone"/></th>
<th>&nbsp;</th>
<th>&nbsp;</th>
</tr>
<c:forEach items="${studentsList}" var="student">
<tr>
    <td>${student.lastname}</td>
    <td>${student.firstname}</td>
    <td>${student.email}</td>
    <td>${student.phone}</td>
    <td><a href="delete/${student.id}"><spring:message code="label.remove"/></a></td>
    <td><a href="studentcourses/${student.id}"><spring:message code="label.courses"/>         
</a></td>    </tr>
</c:forEach>
</table>
</c:if>

............................ 点击课程链接课程页面,其中一个下拉列表包含课程表中的课程列表,我们将在此处选择课程并将其分配给学生。

courses.jsp
.............

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<h2>Courses Manager</h2>

<form:form method="post" action="addcourse.html" commandName="course">

<table>
<tr>
    <td><form:label path="room"><spring:message code="label.room"/></form:label></td>
    <td><form:input path="room"/></td>
</tr>
<tr>
    <td><form:label path="name"><spring:message code="label.name"/></form:label></td>
    <td><form:input path="name"/></td>
<tr>
    <td colspan="2">
        <input type="submit" value="<spring:message code="label.add"/>"/>
    </td>
</tr>
</table>
</form:form>

<h3>Courses</h3>
<c:if  test="${!empty coursesList}">
<table class="data">
<tr>
<th><spring:message code="label.name"/></th>
<th><spring:message code="label.room"/></th>
<th></th>
</tr>
<c:forEach items="${coursesList}" var="course">
<tr>
    <td>${course.name }</td>
    <td>${course.room}</td>
    <td><a href="courses/delete/${course.id}">delete</a></td>
</tr>

</c:forEach>
</table>
</c:if>
<c:if test="${!empty students}">
<table class="data">
<tr>
<th><spring:message code="label.firstname"></spring:message></th>
<th><spring:message code="label.lastname"></spring:message></th>
<th><spring:message code="label.email"></spring:message></th>
<th><spring:message code="label.telephone"></spring:message></th>
</tr>
<c:forEach items="${students}" var="student">
<tr>
<td>
${student.firstname}
</td>
<td>
${student.lastname}
</td>
<td>
${student.email}
</td>
<td>
${student.phone}
</td>

</tr>
</c:forEach>
</table>
</c:if>

将在此jsp课程列表中显示...
我的问题如何在特定课程中显示学生名单。
我试图根据课程让学生使用以下代码..............

From Course c

在这里,我得到了特定课程的学生的正确细节,但我没有将它们显示为

course name.....>student1
           .....>student2
           ......>student3

就像我想要显示细节......任何人都可以建议我实现这个......

1 个答案:

答案 0 :(得分:0)

$ {course.followedBy}应返回当前课程下的学生列表。然后你就可以把它弄清楚了

...
<c:forEach items="${coursesList}" var="course">
...
<c:forEach items="${course.followedBy}" var="student">
</c:forEach>

</c:forEach>
...