我有一个显示教师列表的 GUI。两个由用户选择 - 他们将成为创建的新学校班级的教师。老师-班级的关系是nm。
学校类:(它从其组继承其id)
@Entity
@Table(name="school_classes")
@Cacheable(true)
public class SchoolClass extends Group{
@ManyToMany(cascade=CascadeType.ALL, mappedBy="classes", fetch=FetchType.EAGER)
private SortedSet<Teacher> teachers;
老师:
@Entity
@Table(name="teachers")
@Cacheable(true)
public class Teacher extends creator.models.school.Entity{
// name etc
@ManyToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinTable(name="class_teachers",
joinColumns = @JoinColumn(name="teacher_id", referencedColumnName="id"),
inverseJoinColumns = @JoinColumn(name="class_id", referencedColumnName="id"))
private SortedSet<SchoolClass> classes;
我尝试像这样创建一个新类:
String className = request.getParameter("class_name");
String id1 = request.getParameter("class_teacher1");
String id2 = request.getParameter("class_teacher2");
Teacher t1 = DataRetrieveModule.getTeacher(id1);
Teacher t2 = DataRetrieveModule.getTeacher(id2);
Layout l = new Layout();
SchoolClass newClass = new SchoolClass(className);
newClass.setLayout(l);
newClass.addTeacher(t1);
t1.addClass(newClass);
newClass.addTeacher(t2);
t2.addClass(newClass);
DataInsertionModule.insert(newClass);
此语句DataRetrieveModule.getTeacher(id1)
打开一个会话,通过 ID 检索教师并关闭它。
DataInsertionModule.insert(newClass)
还打开一个会话,调用 session.saveOrUpdate(newClass)
. (我也试过session.merge(newClass)
)
找老师:
public static Teacher getTeacher(String id) {
Session session = null;
Teacher t = null;
try{
sessionFactory = MyFactory.getSessionFactory();
session = sessionFactory.openSession();
t = (Teacher) session.get(Teacher.class, Long.parseLong(id));
}
catch(Exception e) {
System.out.println("in DAO:");
e.printStackTrace();
if(session!=null)
session.close();
}
finally{
if(session!=null)
session.close();
}
return t;
}
数据插入:
public static void insert(Object o) {
Session session = null;
try
{
sessionFactory = MyFactory.getSessionFactory();
session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save(o);
tx.commit();
System.out.println("insertion done");
}
catch (Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
finally
{
if(session!=null)
session.close();
}
}
但是插入永远不会起作用。总是有一个已经存在的具有相同 id 的对象。
a different object with the same identifier value was already associated with the session: [creator.models.school.Teacher#3]
我在 stackoverflow 上进行了搜索,并在类中覆盖了我的 getHashCode 和 equals 方法,我的所有业务对象都继承自:
@Override
public int compareTo(Entity o) {
if(this.id < o.getId())
return -1;
else if(this.id > o.getId())
return 1;
return this.getClass().getName().compareTo(o.getClass().getName());
}
@Override
public boolean equals(Object o) {
if(o instanceof Entity)
return this.compareTo((Entity)o)==0;
else return this.equals(o);
}
@Override
public int hashCode() {
char[] bla = this.getClass().getName().toCharArray();
int blue=0;
for(char c:bla)
blue = blue*10 + c;
return (int) (id+blue);
}
此外,我尝试将 of 更改CascadeType
为ManyToMany
(MERGE
再次还原)。
目前,我在使用 session.get(..) 检索两个教师对象后合并教师对象,因为检索会从数据库中取出大量数据。还有进一步的 ..ToMany
关系。因此,可能会发生调用导致两个教师都被加载的情况。
public static Object merge(Object o) {
Session session = null;
Object returnVal = null;
try
{
sessionFactory = MyFactory.getSessionFactory();
session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
returnVal = session.merge(o);
tx.commit();
}
catch (Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}
finally
{
if(session!=null)
session.close();
}
return returnVal;
}
.
Teacher t1 = DataRetrieveModule.getTeacher(id1);
Teacher t2 = DataRetrieveModule.getTeacher(id2);
t1= DataInsertionModule.merge(t1);
t2= DataInsertionModule.merge(t2);
因此,我想,如果我将我的 get 方法返回的那个与另一个老师的 get 调用必须加载的那个合并,它应该可以工作。(像这里: http: //www.stevideter.com/2008/12/07/saveorupdate-versus-merge-in-hibernate/)但它没有:(
可能是因为同一个超类(对象或我的实体类)的对象具有相同的 ID?
请帮我!