我不知道如何将学生添加到学生组,我的代码如下所示:
型号:
组
@Entity
@Table(name = "groups")
public class Groups extends BaseEntitity {
@Column(name = "group_name", nullable = false, length = 20)
private String GroupName;
学生:
@Entity
@Table(name = "student")
public class Student extends BaseEntitity{
@Column(name = "name", nullable = false, length = 35)
private String name;
@Column(name = "last_name", nullable = false, length = 20)
private String last_name;
@Column(name = "birth_date", nullable = false)
private String birth_date;
@Column(name = "city", nullable = false, length = 30)
private String city;
学生团体:
@Entity
@Table(name="student_group")
public class StudentGroup extends BaseEntitity {
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "id_student", nullable = false)
private Student student;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "id_group", nullable = false)
private Group group;
我想通过点击学生旁边的按钮将学生添加到群组,转到带有群组的页面,然后点击选定群组旁边的按钮,将选定的学生添加到此群组中,如下所示:
<td>${student.birthDate}</td>
<td>${student.city}</td>
<td> <a href="${goToGroupURL}" class="btn btn-primary">Add Group</a></td>
<c:forEach items="${groupList}" var="group">
<tr>
<td>${group.id}</td>
<td>${group.GroupName}</td>
<td> <a href="${addGroupURL}/${group.id}" class="btn btn-primary">Add Group</a></td>
</tr>
</c:forEach>
服务:
@Service
public class AddGroupServiceImpl implements AddGroupService {
@Autowired
private StudentGroupDAO studentGroupDAO;
@Autowired
private GroupDAO groupDAO;
@Override
@Transactional
public void addStudentToGroup(Student student, Group Group) {
StudentGRoup studentGroup = new StudentGroup(student, group);
studentGroupDAO.save(studentGroup);
groupDAO.save(group);
控制器:
@RequestMapping(value = "/add-student/group/{groupId}")
public String createStudentGroup(@PathVariable Long groupId, Lond studentId, Principal principal, RedirectAttributes redirectAttributes) {
Group group = groupDao.findOne(groupId);
Student student = studentDao.findOne(studentId);
addGroupService.addStudentToGroup(student, group);
StudentGroupDAO:
@Repository public interface StudentGroupDAO extends JpaRepository<StudentGroup, Long> { }
我不知道如何在控制器中执行此操作,有人可以帮助jsp吗?
答案 0 :(得分:0)
总的来说,这个概念似乎很好。问题是您将分离的实体传递给持久性提供程序以进行保存。
尝试将id传递给事务服务并在那里执行查询:
@Override
@Transactional
public void addStudentToGroup(Integer studentId, Integer groupId) {
Group group = groupDao.findOne(groupId);
Student student = studentDao.findOne(studentId);
StudentGrupa studentGrupa = new StudentGrupa(student, grupy);
studentGroupDAO.save(studentGroup);
groupDAO.save(group);
}
不确定studentGroupDAO.save
内部发生了什么,但现在您将保存托管实体。