views:

401

answers:

4

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)

A: 

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();
Pascal Thivent
+1  A: 

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.

Barthelemy
yes. it works with transactions. but why not without ? i did a much simpler project, and there i hadn't had to use transactions because it worked only with flush().
Blitzkr1eg
You could be using Application managed transactions ex that Seam, MakeFaces etc... support in which each method is wrapped in transaction using AOP or other mechanizem and committed when the method executes without any problem or rolled back when there is exception. Take a look at session-per-request pattern if you don't want to wrap all your code in TX.
Greg
Did you use the same database with the same configuration? Was the autocommit mode on (on the Hibernate side)? Depending on the database, you can disable transaction support (or enable autocommit) when configuring the database. More information would be needed to help you pinpoint the difference.
Barthelemy
What do you mean 'same database with same configuration' ?The autocommit mode i think it was Off on the hibernate side, because i'm new to hibernate and just now found out about that mode.Thanks for your help.
Blitzkr1eg
I mean, what database did you use (mysql, sqlite, postgres) in both cases (when it worked and when it did not work)? How did you create the database in both cases?
Barthelemy
A: 

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

Sebastien Lorber
A: 

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.

Poso Lingofe