Hibernate : Problème d'identifiant
Bonjour à tous.
Je sollicite les experts Hibernate car j'ai un un problème que je ne comprends pas. Je découvre Hibernate et je tente d'insérer un objet dans une base Oracle : l'enregistrement se passe bien mais je me retrouve avec un décalage de 1 entre l'identifiant retourné par session.save(monObjet) et l'enregistrement crée en base de données.
(En fait, qand session.save() me retourne par exemple 49, mon enregistrement dans la BDD porte le N°50 ).
Mon fichier de mapping :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 22 mai 2013 08:40:30 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.hibernate.Affectation" table="AFFECTATION">
<id name="noaffect" type="short">
<column name="NOAFFECT" precision="3" scale="0" />
<generator class="native">
<param name="sequence">SILOSEQ</param>
</generator>
</id>
<property name="lib" type="string">
<column name="LIB" length="60" not-null="true" />
</property>
<property name="liba" type="string">
<column name="LIBA" length="10" />
</property>
</class>
</hibernate-mapping> |
Mon fichier de config :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.password">.....</property>
<property name="..."</property>
<property name="hibernate.connection.username">....</property>
<property name="hibernate.default_schema">.....</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- Formater sql -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- List of XML mapping files -->
<mapping resource="com/Hibernate/Affectation.hbm.xml"/>
<mapping resource="com/Hibernate/Fonction.hbm.xml"/>
</session-factory>
</hibernate-configuration> |
Et enfin mon fichier de test :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
package com.test;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.hibernate.Affectation;
public class Test {
private static SessionFactory factory;
public static void main(String[] args) {
try {
factory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
Test test = new Test();
// Creation nouveau Silo
System.out.println("nouveau silo crée :"
+ test.ajouterSilo("nouveauSilo"));
}
public Short ajouterSilo(String nomSilo) {
Session session = factory.openSession();
Transaction tx = null;
Short siloID = null;
try {
tx = session.beginTransaction();
Affectation newSilo = new Affectation();
newSilo.setLib(nomSilo);
System.out.println("avant save : " + newSilo.getNoaffect());
siloID = (Short) session.save(newSilo);
System.out.println("apres save : " + newSilo.getNoaffect());
tx.commit();
} catch (HibernateException e) {
System.out.println("oups");
if (tx != null)
tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
return siloID;
}
} |
D'avance un grand merci à toute personne qui puisse me mettre sur la voie :oops:...