【问题标题】:how to fetch the data from parent table data based on child table data in many to many relationship in bidirectional using hibernate and spring如何使用hibernate和spring在多对多关系中基于子表数据从父表数据中获取数据
【发布时间】:2013-08-24 06:32:27
【问题描述】:

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

学生桌
.....
ID(主键)
名字
姓氏
电子邮件
电话

课程表

列名
......
ID(主键)
房间
姓名

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

学生课程
.............
列名
..........
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;
}

}

这里的关系正常...

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

就像我想显示细节...谁能建议我实现这个...

【问题讨论】:

    标签: spring-mvc


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-15
      相关资源
      最近更新 更多