我用Java中的Hibernate映射了一些類到一些表中,我設置Hibernate顯示SQL,它打開會話,它顯示它執行SQL,它關閉會話,但沒有修改數據庫。休眠不會保存到數據庫
實體
public class Profesor implements Comparable<Profesor> {
private int id;
private String nume;
private String prenume;
private int departament_id;
private Set<Disciplina> listaDiscipline; //the teacher gives some courses}
public class Disciplina implements Comparable<Disciplina>{ //the course class
private int id;
private String denumire;
private String syllabus;
private String schNotare;
private Set<Lectie> lectii;
private Set<Tema> listaTeme;
private Set<Grup> listaGrupuri; // the course gets teached/assigmened to some groups of students
private Set<Assignment> listAssignments;}
映射
<hibernate-mapping default-cascade="all">
<class name="model.Profesor" table="devgar_scoala.profesori">
<id name="id" column="id">
<generator class="increment"/>
</id>
<set name="listaDiscipline" table="devgar_scoala.`profesori-discipline`">
<key column="Profesori_id" />
<many-to-many class="model.Disciplina" column="Discipline_id" />
</set>
<property name="nume" column="Nume" type="string" />
<property name="prenume" column="Prenume" type="string" />
<property name="departament_id" column="Departamente_id" type="integer" />
</class>
<class name="model.Grup" table="devgar_scoala.grupe">
<id name="id" unsaved-value="0">
<generator class="increment"/>
</id>
<set name="listaStudenti" table="devgar_scoala.`studenti-grupe`">
<key column="Grupe_id" />
<many-to-many column="Studenti_nrMatricol" class="model.Student" />
</set>
<property name="nume" column="Grupa" type="string"/>
<property name="programStudiu" column="progStudii_id" type="integer" />
</class>
<class name="model.Disciplina" table="devgar_scoala.discipline" >
<id name="id" >
<generator class="increment"/>
</id>
<property name="denumire" column="Denumire" type="string"/>
<property name="syllabus" type="string" column="Syllabus"/>
<property name="schNotare" type="string" column="SchemaNotare"/>
<set name="listaGrupuri" table="devgar_scoala.`Discipline-Grupe`">
<key column="Discipline_id" />
<many-to-many column="Grupe_id" class="model.Grup" />
</set>
<set name="lectii" table="devgar_scoala.lectii">
<key column="Discipline_id" not-null="true"/>
<one-to-many class="model.Lectie" />
</set>
</class>
唯一的 '搞笑' 的事情是,PROFESOR對象被加載不能與/由Hibernate但手動經典SQL的Java。這就是爲什麼我保存PROFESOR對象這樣
p - 手動加載PROFESOR對象
Profesor p2 = (Profesor) session.merge(p);
session.saveOrUpdate(p2);
//flush session of course
在此之後,我得到了Java控制檯:
Hibernate: insert into devgar_scoala.grupe (Grupa, progStudii_id, id) values (?, ?, ?)
但是當我看着數據庫表Grupe(組表)
您使用的是交易嗎?如果是這樣,請確保您正在進行交易 – 2010-04-05 10:45:13
您是否使用交易控制? – marcosbeirigo 2010-04-05 10:46:47