Hibernate不会保存到数据库

发布于 2024-08-28 05:30:05 字数 3042 浏览 3 评论 0原文

我在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 (组表)中没有新行

I mapped some classes to some tables with hibernate in Java, i set Hibernate to show SQL, it opens the session, it shows that it does the SQL, it closes the session but there are no modifications to the database.

Entity

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;}

Mapping

<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>

The only 'funny' thing is that the Profesor object gets loaded not with/by Hibernate but with manual classic SQL Java. Thats why i save the Profesor object like this

p - the manually loaded Profesor object

Profesor p2 = (Profesor) session.merge(p);
session.saveOrUpdate(p2);


//flush session of course

After this i get in the Java console:

Hibernate: insert into devgar_scoala.grupe (Grupa, progStudii_id, id) values (?, ?, ?)

but when i look into the database there are no new rows in the table Grupe (the Groups table)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月朦胧 2024-09-04 05:30:05

正如评论中提到的,我的猜测是您没有提交更改。您可以尝试类似的操作:

Transaction tx = null;
try {
    tx = session.beginTransaction()
    Profesor p2 = (Profesor) session.merge(p);
    session.saveOrUpdate(p2);
    tx.commit();
} catch(Exception e) {
    tx.rollback();
}

您还可以在配置文件中使用自动提交模式来避免手动提交更改。在 Hibernate 参考。不过,我认为并非所有数据库都(很好)支持自动提交。

As mentioned in the comments, my guess is that you are not committing the changes. You could try something like that:

Transaction tx = null;
try {
    tx = session.beginTransaction()
    Profesor p2 = (Profesor) session.merge(p);
    session.saveOrUpdate(p2);
    tx.commit();
} catch(Exception e) {
    tx.rollback();
}

You could also use the auto-commit mode in your configuration file to avoid manually committing your changes. Look for the "hibernate.connection.autocommit" property in the Hibernate reference. I don't think autocommit is supported (well) by all databases though.

遗失的美好 2024-09-04 05:30:05

将此代码片段添加到您的休眠配置中:

<属性名称 = "current_session_context_class">线程

使用此代码:

Session session = factory.getCurrentSession();
Transaction tx = session.beginTransaction();

Profesor p2 = (Profesor) session.merge(p);
session.saveOrUpdate(p2);

tx.commit();

请注意,合并不是必需的,您可以只调用 saveOrUpdate,因为如果对象具有标识符,则此方法在内部不会进行合并。

Add this snippet to your hibernate configuration:

<property name = "current_session_context_class">thread</property>

this use this code:

Session session = factory.getCurrentSession();
Transaction tx = session.beginTransaction();

Profesor p2 = (Profesor) session.merge(p);
session.saveOrUpdate(p2);

tx.commit();

Note that the merge is not necessary, you could just call saveOrUpdate, because this method internally does na merge if the object has an identifier.

故人如初 2024-09-04 05:30:05

确保您在事务中运行代码(在最后提交):

Session session = factory.openSession();
Transaction tx = session.beginTransaction();

Profesor p2 = (Profesor) session.merge(p);
session.saveOrUpdate(p2);

tx.commit();
session.close();

Make sure you run your code inside a transaction (that you commit at the end):

Session session = factory.openSession();
Transaction tx = session.beginTransaction();

Profesor p2 = (Profesor) session.merge(p);
session.saveOrUpdate(p2);

tx.commit();
session.close();
清眉祭 2024-09-04 05:30:05

如果它不适用于事务,那很可能是因为事务是在你的mysql(innoDB引擎)上激活的,所以你只需要在你的mysql服务器上提交或禁用事务......

但无论如何,保持事务不存在并不是一个坏主意? ?

If it doesn't work with transactions it's prolly because transactions are activated on your mysql (innoDB engine) so you just have to commit or disable transactions on your mysql server...

but anyway it's not a bad idea to keep transactions no???

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文