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();
}
}
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.
Cette action est irreversible, confirmez la suppression du commentaire ?
Signaler le commentaire
Veuillez sélectionner un problème
Nudité
Violence
Harcèlement
Fraude
Vente illégale
Discours haineux
Terrorisme
Autre
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
Parce que si je me souviens bien, class="vm" est obsolète
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
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