Spring mvc 如何使用hibernate和spring在双向多对多关系中基于子表数据从父表数据中获取数据
我在oracle数据库的多对多关系中有两个如下表。Spring mvc 如何使用hibernate和spring在双向多对多关系中基于子表数据从父表数据中获取数据,spring-mvc,Spring Mvc,我在oracle数据库的多对多关系中有两个如下表。 学生表 ……… ID(主键) 名字 姓氏 电子邮件 电话 课程表 列名 ……… ID(主键) 房间 名字 这是我的两个表,中间表是这样的 学生课程 ……… 列名 ……. 学生ID(学生表中的主键和外键) 课程ID(课程表中的主键和外键) 我有两个实体类 Student.java …… import javax.persistence.*; import java.util.*; @Entity @Table(name = "
学生表
………
ID(主键)
名字
姓氏
电子邮件
电话 课程表 列名
………
ID(主键)
房间
名字 这是我的两个表,中间表是这样的
学生课程
………
列名
…….
学生ID(学生表中的主键和外键)
课程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;
}
<%@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> </th>
<th> </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>
<%@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>
From Course c
在这里,我获得了特定课程下学生的正确详细信息,但我未能将其显示为course name.....>student1
.....>student2
......>student3
像这样,我想显示详细信息…谁能建议我实现这一点…${course.followerdby}应该返回当前课程下的学生列表。然后你可以给它打分
...
<c:forEach items="${coursesList}" var="course">
...
<c:forEach items="${course.followedBy}" var="student">
</c:forEach>
</c:forEach>
...
。。。
...
...
...
<c:forEach items="${coursesList}" var="course">
...
<c:forEach items="${course.followedBy}" var="student">
</c:forEach>
</c:forEach>
...