Java 休眠多个条目

Java 休眠多个条目,java,hibernate,equality,Java,Hibernate,Equality,我正在使用Hibernate 4.3。 我为学生创建了以下实体 @Entity @Table(name="STUDENT") public class Student { public Student(){ } public Student(String name, Set<Course> courses){ this.studentName = name; this.courses = courses; }

我正在使用Hibernate 4.3。
我为
学生
创建了以下实体

@Entity
@Table(name="STUDENT")
public class Student {

    public Student(){
    }

    public Student(String name, Set<Course> courses){
        this.studentName = name;
        this.courses = courses;
    }

    @Id
    @GeneratedValue
    @Column(name="STUDENT_ID")
    private long studentid;
    @Column(name="STUDENT_NAME")
    private String studentName;
    @ManyToMany(cascade=CascadeType.ALL)
    @JoinTable(name="STUDENT_COURSE",
        joinColumns=@JoinColumn(name="STUDENT_ID"),
        inverseJoinColumns=@JoinColumn(name="COURSE_ID")
    )
    private Set<Course> courses = new HashSet<Course>(0);

    //Getter Setter Methods
}
在我的应用程序中,我的代码为:

// Configuration and Session creation for Hibernate

Set<Course> courses = new HashSet<Course>();
courses.add(new Course("Maths"));
Student st1 = new Student("ABCD", courses);
session.save(st1);

courses.add(new Course("Physics"));
Student st2 = new Student("EFGH", courses);
session.save(st2);
//Hibernate的配置和会话创建
Set courses=newhashset();
课程。增加(新课程(“数学”);
学生st1=新学生(“ABCD”,课程);
session.save(st1);
增加(新课程(“物理”);
学生st2=新生(“EFGH”,课程);
session.save(st2);
在上述情况下,它插入无效数据,因为这两门课程都是针对两个学生的。 这是不正确的,但在Java中,对象是相同的,因此是正确的。 但是我想让课程按照上面的定义进行映射。如何在休眠结束时处理这个问题

我尝试了另一种选择:

Set<Course> courses = new HashSet<Course>();
Set<Course> courses1 = new HashSet<Course>();
courses.add(new Course("Maths"));   
Student st1 = new Student("ABCD", courses);

session.save(st1);

courses1.add(new Course("Maths"));
courses1.add(new Course("Physics"));
Student st2 = new Student("EFGH", courses1);
session.save(st2);
Set courses=newhashset();
Set courses1=新HashSet();
课程。增加(新课程(“数学”);
学生st1=新学生(“ABCD”,课程);
session.save(st1);
课程1.增加(新课程(“数学”);
课程1.增加(新课程(“物理”);
学生st2=新生(“EFGH”,课程1);
session.save(st2);
但这次它为同一个
courseName=“math”
创建了两个不同的课程。 即使我已经创建了
equals
hashCode
方法实现


需要解决方案,如何在Hibernate中处理此问题。

我相信您的问题来自
课程中
equals()
方法的实现

事实上,当你这样做时:

return this.courseName == anotherCourse.courseName;
,您可以将
此.courseName
的内存引用与
另一个课程的
courseName
进行比较

相反,您应该使用equals()方法,如下所示:

return this.courseName.equals(anotherCourse.courseName);
,它比较字符串内容而不是引用


在您的第二次尝试中,由于您创建了两个对象(具有相同的值),并且由于您在
课程中实现的
equals()
比较了对象的引用(内存地址)而不是它们的内容,Hibernate认为它们是不同的。

我找到了解决问题的方法,如下所示:

Course maths = new Course("Maths");
Course physics = new Course("Physics");

Set<Course> courses = new HashSet<Course>();
Set<Course> courses1 = new HashSet<Course>();

courses.add(maths);
Student st1 = new Student("ABCD", courses);
session.save(st1);

courses1.add(maths);
courses1.add(physics);
Student st2 = new Student("EFGH", courses1);
session.save(st2);
课程数学=新课程(“数学”);
课程物理=新课程(“物理”);
Set courses=newhashset();
Set courses1=新HashSet();
课程。加上(数学);
学生st1=新学生(“ABCD”,课程);
session.save(st1);
课程1.增加(数学);
课程1.增加(物理);
学生st2=新生(“EFGH”,课程1);
session.save(st2);
在这里,我为
课程
创建了对象,并在两个集合中使用了相同的对象。因此,在数据库中,只为course
Mathematics
创建了一行条目

这样就解决了问题。
谢谢。

我修改了行
返回this.courseName==anotherCourse.courseName
to
return(this.courseName==null)?anotherCourse.courseName==null:this.courseName.equals(anotherCourse.courseName)。但它仍然不起作用,它正在课程表中插入两条
数学
记录。如果需要它起作用,请先保存所有课程,然后将它们分配给学生并保存学生。无需先保存所有
课程
。只是这两门课程都要求有相同的对象。我的错误是,我做了两次新课程(“数学”)
。我在问题中添加了关于相同内容的评论。你的解决方案是一个非常糟糕的主意。您通过重用同一个对象来解决问题,但您并没有解决它。如果你保持这种方式,你将不可避免地在未来有错误。如果您必须从数据库中检索它,然后附加,您会怎么做??您的equals实现是错误的,不要因为无效的琐碎方法实现而修改您的biz代码!我的错误,似乎我读了你的“第二个选项”代码而不是解决方案代码:)现在你真的很好!请注意,修复equals()对于未来非常重要-现在您可以比较表示同一课程的两个课程对象,即使这两个对象在内存中不相同,这在您的应用程序变得更复杂时不可避免地会发生:)基本上,您现在比较的是一个
唯一的业务密钥
(假设课程名称是唯一的),这是
最佳实践
Course maths = new Course("Maths");
Course physics = new Course("Physics");

Set<Course> courses = new HashSet<Course>();
Set<Course> courses1 = new HashSet<Course>();

courses.add(maths);
Student st1 = new Student("ABCD", courses);
session.save(st1);

courses1.add(maths);
courses1.add(physics);
Student st2 = new Student("EFGH", courses1);
session.save(st2);