我用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中没有新行(Groups表)
答案 0 :(得分:3)
正如评论中所提到的,我的猜测是你没有提交更改。你可以试试这样的东西:
Transaction tx = null;
try {
tx = session.beginTransaction()
Profesor p2 = (Profesor) session.merge(p);
session.saveOrUpdate(p2);
tx.commit();
} catch(Exception e) {
tx.rollback();
}
您还可以在配置文件中使用自动提交模式,以避免手动提交更改。在Hibernate reference中查找“hibernate.connection.autocommit”属性。我不认为所有数据库都支持(好)自动提交。
答案 1 :(得分:3)
将此代码段添加到您的休眠配置中:
<property name = "current_session_context_class">thread</property>
这使用此代码:
Session session = factory.getCurrentSession();
Transaction tx = session.beginTransaction();
Profesor p2 = (Profesor) session.merge(p);
session.saveOrUpdate(p2);
tx.commit();
请注意,合并不是必需的,您可以只调用saveOrUpdate,因为如果对象具有标识符,则此方法在内部执行na合并。
答案 2 :(得分:0)
确保在事务中运行代码(最后提交的代码):
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
Profesor p2 = (Profesor) session.merge(p);
session.saveOrUpdate(p2);
tx.commit();
session.close();
答案 3 :(得分:0)
如果它不能与事务一起使用,那么因为在mysql(innoDB引擎)上激活了事务,所以你只需要在mysql服务器上提交或禁用事务......
但无论如何保持交易不是一个坏主意