Bonjour,

Je galère depuis plusieurs jours pour faire mon propre générateur d'ID qui est assez simple le seul problème c'est que ce générateur doit sauvegarder les id dans une base de donnée différente de celle d'objet persistant parce que mon application utilise 2 datasources.

voici une partie de mon persistence-application-context :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
 
	<!--
	====================================================== 
	BASE CENTRALE
	====================================================== 
	 -->
	<bean id="sessionFactoryCentrale"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSourceCentrale" />
		<property name="mappingResources">
			<list>
				<value>fr/laser/loyalty/fid/ruler/persistence/adm/habilitation/maps/Profil.hbm.xml</value>
				<value>fr/laser/loyalty/fid/ruler/persistence/adm/habilitation/maps/Fonctionnalite.hbm.xml</value>
    			<value>fr/laser/loyalty/fid/ruler/persistence/central/maps/ProgrammeFid.hbm.xml</value>
 
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					fr.laser.loyalty.fid.ruler.persistence.fmk.dialect.DB2Dialect
				</prop>
				<prop key="hibernate.max_fetch_depth">2</prop>
				<!-- for developpement uncomment 
				<prop key="hibernate.show_sql">true</prop>
				-->
			</props>
		</property>
	</bean>
 
	<!-- ******* Définitions abstraites pour servir de base aux DAOs ******* -->
	<bean id="abstractDAOCentrale" abstract="true">
		<property name="sessionFactory" ref="sessionFactoryCentrale" />
	</bean>
 
	<!-- ******* Liste des DAOs ******* -->
	<bean id="profilDAO"
		class="fr.laser.loyalty.fid.ruler.persistence.adm.habilitation.impl.ProfilDAOImpl"
		parent="abstractDAOCentrale">
	</bean>
 
	<bean id="programmeFidDAO"
		class="fr.laser.loyalty.fid.ruler.persistence.central.impl.ProgrammeFidDAOImpl"
		parent="abstractDAOCentrale">
	</bean>
 
	<!--
	====================================================== 
	BASE PROPRIETAIRE
	====================================================== 
	 -->
	<bean id="sessionFactoryProprietaire"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSourceProprietaire" />
		<property name="mappingResources">
			<list>
				<value>fr/laser/loyalty/fid/ruler/persistence/param/generique/maps/PaysDTO.hbm.xml</value>
				<value>fr/laser/loyalty/fid/ruler/persistence/param/generique/maps/DomaineGeneriqueDTO.hbm.xml</value>
				<value>fr/laser/loyalty/fid/ruler/persistence/param/generique/maps/ParametreGeneriqueDTO.hbm.xml</value>
				<value>fr/laser/loyalty/fid/ruler/persistence/param/generique/maps/ValeurParametreGeneriqueDTO.hbm.xml</value>
				<value>fr/laser/loyalty/fid/ruler/persistence/param/generique/maps/SocieteDTO.hbm.xml</value>
				<value>fr/laser/loyalty/fid/ruler/persistence/param/generique/maps/ParametrageDTO.hbm.xml</value>
				<value>fr/laser/loyalty/fid/ruler/persistence/proprietaire/regle/maps/RegleVO.hbm.xml</value>
				<value>fr/laser/loyalty/fid/ruler/persistence/proprietaire/regle/maps/RegleCaracVO.hbm.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					fr.laser.loyalty.fid.ruler.persistence.fmk.dialect.DB2Dialect
				</prop>
				<prop key="hibernate.max_fetch_depth">2</prop>
				<!-- for developpement uncomment 
				<prop key="hibernate.show_sql">true</prop>
				-->
			</props>
		</property>
	</bean>
 
	<!-- Règles -->
	<bean id="regleDAO"
		class="fr.laser.loyalty.fid.ruler.persistence.proprietaire.regle.impl.RegleDAOImpl"
		parent="abstractDAOProprietaire">
	</bean>
 
	<bean id="regleCaracDAO"
		class="fr.laser.loyalty.fid.ruler.persistence.proprietaire.regle.impl.RegleCaracDAOImpl"
		parent="abstractDAOProprietaire">
	</bean>

Voici le mapping de mon objet :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
<hibernate-mapping package="fr.laser.loyalty.fid.ruler.valueobject.regle">
 
	<class name="RegleVO" table="REG201V1">
		<id name="numeroRegle" column="NUM_REGL" type="long">
			<!-- <generator class="assigned"/> -->
			<generator class="fr.laser.loyalty.fid.ruler.persistence.fmk.key.generator.CustomGenerator"/>
		</id>		
		<property name="numeroProgramme" column="NUM_PROG" type="int" not-null="true"/>
		<property name="codeModele" column="CODE_MODELE" type="string" length="10" not-null="true"/>
		<property name="libelleLong" column="LIBC_REG" type="string" length="40" not-null="true"/>
		<property name="libelleCourt" column="LIBL_REG" type="string" length="500" not-null="true"/>
		<property name="formule" column="FORMULE" type="string" length="4096" not-null="true"/>
		<property name="disponibilite" column="REG_DISPO" type="int" not-null="true"/>
		<property name="signatureCreateur" column="SIG_CRE" type="string" length="10" not-null="true"/>
		<property name="dateCreation" column="DATE_CRE" type="calendar" not-null="true"/>
		<property name="signatureDerniereMAJ" column="SIG_MAJ" type="string" length="10"/>
		<property name="dateDerniereMAJ" column="DATE_MAJ" type="calendar"/>
	</class>
 
	<query name="getReglesByNumeroProgrammeQuery">
		from RegleVO where numeroProgramme = :numeroProgramme
	</query>
 
</hibernate-mapping>

Voici le code source de mon générateur d'ID :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
 
public class CustomGenerator implements IdentifierGenerator, Configurable
{
    protected static final Logger log = Logger.getLogger(CustomGenerator.class);
 
    private String tableName;
 
    /**
     * This method will generate a random number and return it, hibernate can use this id as it generator class id. {@inheritDoc}
     */
    @Override
    public Serializable generate(SessionImplementor session, Object object) throws HibernateException
    {
        if (log.isDebugEnabled())
            log.debug("generate - DEBUT");
 
        long indice = 0;
 
        Statement statement = null;
        ResultSet rs = null;
 
        try 
        {
            statement = session.connection().createStatement();
            rs = statement.executeQuery("SELECT indice_courant FROM REG007V1 where nom_schema = 'REGX' and nom_table = '" + tableName + "'");
 
            if(rs.next()){
                indice = rs.getLong("indice_courant");
 
                if (log.isDebugEnabled())
                    log.debug("generate - indice_courant = "+indice);
 
                indice++;
                statement.executeUpdate("update REG007V1 set indice_courant = " + indice + " where nom_schema = 'REGX' and nom_table = '" + tableName + "'");
            }
            else{
                indice++;
                statement.executeUpdate("insert into REG007V1 (nom_schema, nom_table, indice_courant) values ('REGX', '" + tableName + "', " + indice + ")");
            }
        } catch (SQLException e) {
            log.error("generate - Erreur pendant l'insert ou l'update du nouveau indice ", e);
        } finally {
            try
            {
                if (statement != null)
                    statement.close();
 
                if (rs != null)
                    rs.close();
            } catch (SQLException e) {
                log.error("generate - Erreur pendant la fermeture du Statement ou du ResultSet", e);
            }
        }
 
        if (log.isDebugEnabled())
            log.debug("generate - FIN, indice = " + indice);
 
        return indice;        
    }
 
    /**
     * This method's parameters have all the available details of hbm.xml file.Since all the database table related data available ,Hibernate can access to the each and every table. In this example to get my id i used procedure. To execute my stored procedure i need to pass table name. {@inheritDoc}
     */
    @Override
    public synchronized void configure(Type type, Properties params, Dialect d) throws MappingException
    {
        tableName = params.getProperty(PersistentIdentifierGenerator.TABLE);
    }
}
Merci d'avance pour votre aide.