java Hibernate 不保存值的集合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1356673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate not saving collection of values
提问by Mark
Okay, I've used Hibernate in several projects now but I did not learn its intricacies before using it. I started with looking at codes that used JPA Annotations and integrated with Spring and everything worked well. But now that I want to teach basic Hibernate to my students and I'm in the process of creating an example and using the documentation tutorial Chapter 1, I'm having a problem with saving a Set of collection values in one persistent class.
好的,我现在已经在几个项目中使用了 Hibernate,但是在使用它之前我没有了解它的复杂性。我首先查看使用 JPA 注释并与 Spring 集成的代码,一切运行良好。但是现在我想向我的学生教授基本的 Hibernate 并且我正在创建一个示例并使用文档教程第 1 章,我在一个持久类中保存一组集合值时遇到了问题。
Here's the persistent class...
这是持久类...
public class Student {
private Long id;
private String firstName;
private String lastName;
private Set<Course> courses = new HashSet<Course>();
private Set<String> contactDetails = new HashSet<String>();
//getters and setters
}
The mapping file...
映射文件...
<hibernate-mapping package="com.phoenixone.school.model">
<class name="Student" table="student">
<id name="id" column="studentId">
<generator class="native" />
</id>
<property name="firstName" />
<property name="lastName" />
<set name="courses" table="student_course" lazy="false">
<key column="studentId" />
<many-to-many column="courseId" class="Course" />
</set>
<set name="contactDetails" table="contactDetails">
<key column="studentId" />
<element type="string" column="contactDetail" />
</set>
</class>
</hibernate-mapping>
The DAO (saving part)
DAO(保存部分)
public class StudentDaoHibernate {
public void save(Student student){
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(student);
session.getTransaction().commit();
}
}
The testing part...
测试部分...
public class TestStudentDaoHibernate {
public static void main(String[] args) {
CourseDaoHibernate courseDao = new CourseDaoHibernate();
Course c1 = new Course();
c1.setCourseCode("CWD");
c1.setCourseName("Web Dev");
courseDao.save(c1);
Set<Course> courses = new HashSet<Course>();
courses.add(c1);
Student student = new Student();
student.setFirstName("Bob");
student.setLastName("Santos");
student.setCourses(courses);
student.getContactDetails().add("123456789");
StudentDaoHibernate dao = new StudentDaoHibernate();
dao.save(student);
}
}
When I execute these lines of code...
当我执行这些代码行...
Session session = HibernateUtil.getSessionFactory().getCurrentSession()
session.beginTransaction();
Student student = (Student)session.createCriteria(Student.class)
.add(Restrictions.eq("id", new Long(1))).uniqueResult();
System.out.println("First Name: " + student.getFirstName());
System.out.println("Last Name: " + student.getLastName());
System.out.println("Courses enrolled with...");
for(Course c:student.getCourses()){
System.out.println(c.getCourseName());
}
System.out.println("Contact details...");
for(String s:student.getContactDetails()){
System.out.println(s);
}
What I get is this...
我得到的是这个...
First Name: Bob
Last Name: Santos
Courses enrolled with...
Web Dev
Contact details...
名字:Bob
姓氏:Santos
课程注册...
Web Dev
联系方式...
Which leads me to the conclusion that the String "123456789" was not saved. What do you think is the problem with my code? Thanks!
这使我得出结论,字符串“123456789”没有被保存。你认为我的代码有什么问题?谢谢!
回答by Mark
You need to add a cascade attribute to your set elements for courses and contactDetails. This defines what operations performed on the parent entity will be applied to the child entity. The default value is none which is why when you saved the Student none of the child elements in the sets where saved.
您需要为课程和联系人详细信息的 set 元素添加级联属性。这定义了对父实体执行的操作将应用于子实体。默认值为 none 这就是为什么当您保存 Student 时,保存的集合中没有任何子元素。
Adding cascade="all,delete-orphan" will cover all operations but you can be more restrictive if you wish. See herein the Hibernate 4.3 reference for more details.
添加 cascade="all,delete-orphan" 将涵盖所有操作,但如果您愿意,您可以限制更多。有关更多详细信息,请参阅Hibernate 4.3 参考中的此处。
回答by duffymo
I'd recommend creating a Contract class and using that instead of a String. It's a better abstraction, and the evidence that Hibernate dealt with your Course class just fine suggests that it'll work well with Contract, too.
我建议创建一个 Contract 类并使用它而不是 String。这是一个更好的抽象,并且 Hibernate 可以很好地处理您的 Course 类的证据表明它也可以很好地与 Contract 一起使用。
回答by Stewart
After session.save(student);try adding session.flush();
之后session.save(student);尝试添加session.flush();

