OVH Cloud OVH Cloud

Hibernate 3.0 avec Hibernate Synchronizer 3.1.1

2 réponses
Avatar
olivier.ferrero
Bonjour,

Je tente d'utiliser hibernate pour mapper une table mysql 5.0.

**********************************************************************
Le fichier de conf:
<?xml version=3D'1.0' encoding=3D'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>
<!-- local connection properties -->
<property name=3D"hibernate.connection.url">
jdbc:mysql://127.0.0.1/test
</property>
<property name=3D"hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name=3D"hibernate.connection.username">user</property>
<property name=3D"hibernate.connection.password">user69</property>
<!-- property name=3D"hibernate.connection.pool_size"></property -->
<!-- dialect for MySQL -->
<property name=3D"dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name=3D"hibernate.show_sql">true</property>
<property name=3D"hibernate.transaction.factory_class">
org.hibernate.transaction.JDBCTransactionFactory
</property>
<mapping resource=3D"Agence.hbm" />
</session-factory>
</hibernate-configuration>



*************************************************************
Voici le .hbm:

<?xml version=3D"1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package=3D"com.shark.test.hibernate">
<class
name=3D"Agence"
table=3D"agence"
>
<id
name=3D"Id"
type=3D"integer"
column=3D"id"
>
<generator class=3D"vm"/>
</id>

<property
name=3D"Name"
column=3D"name"
type=3D"string"
not-null=3D"true"
length=3D"45"
/>


</class>
</hibernate-mapping>
**********************************************************

la g=E9n=E9ration des classes ce fait sans soucis...



***********************************************************
Ma classe Test:
package business.hotel;

import java.io.File;
import java.util.Iterator;
import java.util.List;


import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.shark.test.hibernate.base.*;
import com.shark.test.hibernate.dao.*;
import com.shark.test.hibernate.*;

public class Test
{

private static Configuration configuration;
private static SessionFactory sessionFactory;

private static final ThreadLocal threadSession =3D new ThreadLocal();
private static final ThreadLocal threadTransaction =3D new
ThreadLocal();

// Create the initial SessionFactory from the default configuration
files
static {
try {
configuration =3D new Configuration();
sessionFactory =3D configuration.configure(new
File("D:\\dev\\src\\chark\\HibernateTest\\src\\hibernate.cfg.xml")).buildSe=
ssionFactory();
// We could also let Hibernate bind it to JNDI:
// configuration.configure().buildSessionFactory()
} catch (Throwable ex) {
// We have to catch Throwable, otherwise we will miss
// NoClassDefFoundError and other subclasses of Error
ex.printStackTrace();
throw new ExceptionInInitializerError(ex);
}
}

/**
* Returns the SessionFactory used for this static class.
*
* @return SessionFactory
*/
public static SessionFactory getSessionFactory()
{
/*
* Instead of a static variable, use JNDI: SessionFactory
sessions =3D
* null; try { Context ctx =3D new InitialContext(); String
jndiName =3D
* "java:hibernate/HibernateFactory"; sessions =3D
* (SessionFactory)ctx.lookup(jndiName); } catch
(NamingException ex) {
* throw new RuntimeException(ex); } return sessions;
*/
return sessionFactory;
}

/**
* Retrieves the current Session local to the thread. <p/> If no
Session is
* open, opens a new Session for the running thread.
*
* @return Session
*/
public static Session getSession()
{
// With CMT, this should return
getSessionFactory().getCurrentSession()
// and do nothing else
Session s =3D (Session) threadSession.get();
if (s =3D=3D null) {
System.out.println("Opening new Session for this thread.");
s =3D getSessionFactory().openSession();
threadSession.set(s);
}
return s;
}

/**
* Closes the Session local to the thread.
*/
public static void closeSession()
{
// Would be written as a no-op in an EJB container with CMT
Session s =3D (Session) threadSession.get();
threadSession.set(null);
if (s !=3D null && s.isOpen()) {
System.out.println("Closing Session of this thread.");
s.close();
}
}

/**
* Start a new database transaction.
*/
public static void beginTransaction()
{
// Would be written as a no-op in an EJB container with CMT
Transaction tx =3D (Transaction) threadTransaction.get();
if (tx =3D=3D null) {
System.out.println("Starting new database transaction in
this thread.");
tx =3D getSession().beginTransaction();
threadTransaction.set(tx);
}
}

/**
* Commit the database transaction.
*/
public static void commitTransaction()
{
// Would be written as a no-op in an EJB container with CMT
Transaction tx =3D (Transaction) threadTransaction.get();
try {
if (tx !=3D null && !tx.wasCommitted() &&
!tx.wasRolledBack()) {
System.out.println("Committing database transaction of
this thread.");
tx.commit();
}
threadTransaction.set(null);
} catch (HibernateException ex) {
rollbackTransaction();
throw ex;
}
}

/**
* Rollback the database transaction.
*/
public static void rollbackTransaction()
{
// Would be written as a no-op in an EJB container with CMT
(maybe
// setRollBackOnly...)
Transaction tx =3D (Transaction) threadTransaction.get();
try {
threadTransaction.set(null);
if (tx !=3D null && !tx.wasCommitted() &&
!tx.wasRolledBack()) {
System.out.println("Tyring to rollback database
transaction of this thread.");
tx.rollback();
}
} finally {
closeSession();
}
}

public static void main (String[] args){



Session session2 =3D Test.getSession();
Query query =3D session2.createQuery("from
com.shark.test.hibernate.Agence");



for(Iterator it =3D query.iterate();it.hasNext();){
Object[] row =3D (Object[]) it.next();
System.out.println("ID: " + row[0]);

}


Test.closeSession();


Session session =3D Test.getSession();
Transaction tx =3D null;
List result =3D null;
try {
tx =3D session.beginTransaction();
Query q =3D session.createQuery("from
com.shark.test.hibernate.Agence");

result =3D q.list();
System.out.println(result.size());
tx.commit();
}
catch (Exception he) {
if (tx!=3Dnull) tx.rollback();
he.printStackTrace();
}
finally {

Test.closeSession();

}

}

}
***********************************************************************


J'obtiens cela dans la console d'eclipse 3.1:

14:20:36,42114:20:36,43714:20:36,45314:20:36,45314:20:36,64014:20:37,51514:=
20:37,82814:20:37,87514:20:37,87514:20:37,87514:20:37,87514:20:37,87514:20:=
38,09314:20:38,09314:20:38,09314:20:38,12514:20:38,12514:20:39,06214:20:39,=
062org.hibernate.MappingException:
could not instantiate id generator
at
org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFacto=
ry.java:97)
at
org.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.jav=
a:152)
at
org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:183)
at
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1145)
at business.hotel.Test.<clinit>(Test.java:40)
Caused by: org.hibernate.MappingException: could not interpret id
generator strategy: vm
at
org.hibernate.id.IdentifierGeneratorFactory.getIdentifierGeneratorClass(Ide=
ntifierGeneratorFactory.java:108)
at
org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFacto=
ry.java:91)
=2E.. 4 more
java.lang.ExceptionInInitializerError
at business.hotel.Test.<clinit>(Test.java:47)
Caused by: org.hibernate.MappingException: could not instantiate id
generator
at
org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFacto=
ry.java:97)
at
org.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.jav=
a:152)
at
org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:183)
at
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1145)
at
business.hotel.Test.<clinit>(Test.java:40)14:20:39,12514:20:39,14014:20:39,=
15614:20:39,15614:20:39,15614:20:39,15614:20:39,15614:20:39,15614:20:39,156=
14:20:39,15614:20:39,15614:20:39,15614:20:39,15614:20:39,15614:20:39,15614:=
20:39,17114:20:39,17114:20:39,17114:20:39,17114:20:39,17114:20:39,18714:20:=
39,18714:20:39,20314:20:39,20314:20:39,20314:20:39,20314:20:39,51514:20:39,=
546
Caused by: org.hibernate.MappingException: could not interpret id
generator strategy: vm
at
org.hibernate.id.IdentifierGeneratorFactory.getIdentifierGeneratorClass(Ide=
ntifierGeneratorFactory.java:108)
at
org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFacto=
ry.java:91)
=2E.. 4 more
Exception in thread "main"


Premiere question: que repr=E9sente cette s=E9rie de chiffre ?
Deuxi=E8me question: pourquoi cette exception ?


Celui qui m'apportera des r=E9ponses aura toute ma reconnaissance ; )


Merci,


Olivier.


Nota: Ma table agence est tr=E9s simple: id INTEGER PRIMARY KEY et name
un varchar.

2 réponses

Avatar
Zouplaz
Bonjour, je ne sais pas à quoi correspondent ces chiffres mais pour ce qui
est de l'id (et il semble bien que l'exception soit en rapport) tu devrais
essayer plutôt

<id name="id" type="long" column="id">
<generator class="native"/>
</id>

Parce que si je me souviens bien, class="vm" est obsolète
Avatar
Twist
Merci pour ta réponse