0

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

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

课程表

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

这是我有的两张表,中间表是这样的

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

我有两个实体类

学生.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;
}

}


课程.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;
}

}

在这里,关系正常...


student.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>

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

课程.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

像那样,我想显示详细信息...任何人都可以建议我实现这一目标...

4

1 回答 1

0

${course.followedBy} 应该返回当前课程下的学生列表。然后你可以迭代它

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

</c:forEach>
...
于 2013-08-25T08:01:43.093 回答