Hibernate没有保存到ManyToMany中的映射表中

时间:2014-12-11 01:40:59

标签: java spring hibernate spring-mvc

我是hibernate的新手,我正在尝试将数据保存到Student表中并加入表,该表包含学生的复合键和相关的学生技能表。我正在使用spring MVC和一个服务器来填充这些数据。我觉得我在表格或休眠状态下都是错误的映射,但我不确定我哪里出错了。

形式:

<form action="/SpringMVCProject/submitAdmissionForm" method="POST">
    First name: <input type="text" name="studentFname" />
    <br>
    Last name: <input type="text" name="studentLname" />
    <br>
    Date of birth: <input type="text" name="studentDOB" />
    <br>
    Mobile num: <input type="text" name="studentMobile" />
    <br>
      Select skills: <select name="studentActivities.skill_id">
                        <option value="1">Java</option>
                        <option value="2">C++</option>
                        <option value="3">C#</option>
                        <option value="4">.NET</option>
                        <option value="5">Python</option>
                        <option value="6">Ruby</option>
                    </select>
    <br>
    Country: <input type="text" name="studentAddress.country" />
    <br>
    City: <input type="text" name="studentAddress.city" />
    <br>
    Street: <input type="text" name="studentAddress.street" />
    <br>
    Postal code: <input type="text" name="studentAddress.pincode" />
    <br>
    <input type="submit" value="Submit" />


</form>

学生班:

@Entity
public class Student {
    @Id
    @GeneratedValue
    private int studentID;

    private String studentFname;

    @Size(min=5, max=8)
    @Pattern(regexp="[a-z]*")
    private String studentLname;

    @Embedded
    private Address studentAddress;

    @Past
    private Date studentDOB;

    private Long studentMobile;

    @ManyToMany(cascade = CascadeType.ALL)
    private Set<StudentActivities> studentActivities = new HashSet<StudentActivities>();
}

StudentActivities课程:

@Entity
@Table(name="student_skills")
public class StudentActivities {    
    @Id
    @GeneratedValue
    private int skill_id;

    private String skill_name;
}

以下是我将它保存在控制器类中的方法:

@RequestMapping(value = "/submitAdmissionForm", method  =   RequestMethod.POST)
public ModelAndView submitAdmissionForm(@Valid @ModelAttribute("student1") Student student1, BindingResult result)
{
    ModelAndView model;
    if(result.hasErrors())
        model   =   new ModelAndView("AdmissionForm");
    else
    {
        SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();

        session.beginTransaction();
        session.save(student1);
        session.getTransaction().commit();

        session.close();
        sessionFactory.close();

        model = new ModelAndView("AdmissionSuccess");
    }

    return model;
}

所以它当前将所有数据保存到学生表中,但不保存到我的'Student_student_skills'表中。 'student_skills'表中已有数据,所以我不完全确定它是否值得拥有StudentActivities类,因为我实际上并不想将数据输入'student_skills'表?然而,主要担心的是数据实际上并未保存到映射表中。我在哪里错了?

编辑:

我能够通过创建StudentActivities对象并在Student类中创建一个单独的字段以保存技能ID来保存它,我只是用它来设置学生活动中的技能ID,不知道这是不是正确的方法。似乎我见过的大部分教程都是硬编码数据库条目,让hibernate完成工作,但在我的情况下,我已经有了数据条目,并希望从表单中的列表中选择它们。唯一的问题是skill_name值保留为null,因为我没有指定skill_name,我必须对它们进行硬编码,有什么办法可以阻止它更新我的student_skills表吗?

StudentActivities student_skills = new StudentActivities();
student_skills.setSkill_id(student1.getSkillID());
student1.getStudentActivities().add(student_skills);

1 个答案:

答案 0 :(得分:-1)

您可以使用以下内容:

@ManyToMany(cascade = {CascadeType.ALL})
    @JoinTable(name="Student_student_skills", 
                joinColumns={@JoinColumn(name="Student_ID")}, 
                inverseJoinColumns={@JoinColumn(name="Student_Skill_ID")})
private Set<StudentActivities> studentActivities = new HashSet<StudentActivities>();