Java Hibernate对表更新相同的ID语句感到困惑

时间:2014-12-03 14:33:16

标签: java mysql hibernate one-to-many many-to-one

我们有一段时间这个例程......我们正在使用Hibernate and MySQL

Student->School the student table has a relationship with school. But in DB is not mandatory
the FK could be null..

我们这样做了,我知道这不是最好的方法,但是。

currentSession.save(student);
currentSession.save(student.getSchool());
currentSession.flush();
transaction.commit();

我已经检查了Hibernate生成的SQL ..我看到了。

Hibernate: 
insert 
into
    student
    (C24, C01, C02, C03, C04, c07, c08, c09, c10, c11, C12, C13, C14, C15, c16, C17, C18, c19, C20, C21, C22, C23, C25, C26, C27, C28, C29, C30, C31, C32, C33, C34, C35,school_id) 
values
    (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)


 Hibernate: 
 select
    last_insert_id()
 Hibernate: 
 insert 
 into
    school
    (C01, C02, C03, C04, C05, id) 
values
    (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

Hibernate: 
select
    last_insert_id()

这对我来说没关系,学生表是学校专栏NULL后来我看到..

Hibernate: 
update
    student
set
    school_id=? 
where
    ID=?

这可以通过使用NULL来增加一个句子,但对我来说仍然可以。我在这里更新关系。

但下一句话我不明白..为什么Hibernate会做这样的事情..更新它以前插入的相同ID?我们在两个表中使用auto-increment方法。

Hibernate: 
update
    school
set
    id=? 
where
    ID=?

我错过了什么?

0 个答案:

没有答案