These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: AnnotationConfiguration instance is required
PostPosted: Tue Jul 25, 2006 1:33 pm 
Newbie

Joined: Tue Jul 25, 2006 1:18 pm
Posts: 5
Hibernate version:
hibernate-3.2.0.cr2, hibernate-annotations-3.2.0.CR1

Bonjour,

Je commence avec Hibernate. J'arrive à quelque chose qui fonctionne quand j'utilise Le mapping avec les fichiers xml.

Par contre, quand j'essaye les Annotation, ça ne fonctionne pas (voir l'Exception qui suit).

Full stack trace of any exception that occurs:

org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="name.cyrille.kitchenonweb.User"/>
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1519)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1474)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1453)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1427)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1347)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1333)
at name.cyrille.kitchenonweb.HibernateSessionUtil.<clinit>(HibernateSessionUtil.java:38)
at name.cyrille.kitchenonweb.Bienvenue.TestHibernate1(Bienvenue.java:631)
at name.cyrille.kitchenonweb.Bienvenue.buttonTest1_action(Bienvenue.java:597)

hibernate-configuration

<hibernate-configuration>
<session-factory>
<property name="connection.datasource">java:comp/env/jdbc/KitchenMysql</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">create</property> -->

<!-- Mapping files -->
<!-- <mapping resource="User.hbm.xml"/> -->
<!-- ou Mapping Annotations -->
<property name="configurationClass">org.hibernate.cfg.AnnotationConfiguration</property>
<mapping class="name.cyrille.kitchenonweb.User"/>

</session-factory>
</hibernate-configuration>

persistence-configuration

<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="KitchenOnWeb" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/Kitchen"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.password"/>
<property name="hibernate.connection.username" value="root"/>
</properties>
</persistence-unit>
</persistence>

import java.util.logging.Level;
import java.util.logging.Logger;

HibernateSessionUtil source

import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.tool.hbm2ddl.SchemaUpdate;

public class HibernateSessionUtil {

public HibernateSessionUtil() {
}

static Logger logger = Logger.getLogger("HibernateSessionUtil");

private static final AnnotationConfiguration cfg;
private static final SessionFactory sessionFactory;

static {
try {
cfg = new AnnotationConfiguration();
cfg.configure();

new SchemaUpdate(cfg).execute(true, true);

// Create the SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();

} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
logger.log( Level.SEVERE, "HibernateSessionUtil: Initial SessionFactory creation failed." , ex);
throw new ExceptionInInitializerError(ex);
}
}

public static final ThreadLocal session = new ThreadLocal();

public static Session currentSession() {
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}

public static void closeSession() {
Session s = (Session) session.get();
if (s != null)
s.close();
session.set(null);
}
}


Top
 Profile  
 
 Post subject: suite ...
PostPosted: Tue Jul 25, 2006 2:18 pm 
Newbie

Joined: Tue Jul 25, 2006 1:18 pm
Posts: 5
Ok, après avoir relu la doc j'ai changé la classe HibernateSessionUtil pour qu'elle utilise AnnotationConfiguration

static {
try {

sessionFactory = new AnnotationConfiguration().buildSessionFactory();

} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
logger.log( Level.SEVERE, "HibernateSessionUtil: Initial SessionFactory creation failed." , ex);
throw new ExceptionInInitializerError(ex);
}
}

Mais du coup j'ai l'erreur :

org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
at org.hibernate.dialect.DialectFactory.determineDialect(DialectFactory.java:57)
at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:39)
at org.hibernate.cfg.SettingsFactory.determineDialect(SettingsFactory.java:397)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:111)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:1928)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1211)

et pourant le DIALEC est défini dans le fichier de configuration hibernate.cfg.xml ...

Que faire ??
cyrille


Top
 Profile  
 
 Post subject: Re: suite ...
PostPosted: Tue Jul 25, 2006 5:42 pm 
Newbie

Joined: Tue Jul 25, 2006 1:18 pm
Posts: 5
cyrille37 wrote:
sessionFactory = new AnnotationConfiguration().buildSessionFactory();
...
Mais du coup j'ai l'erreur :
org.hibernate.HibernateException: Hibernate Dialect must be explicitly set


J'avance à petit pas. Voilà une modif qui permet à Hibernate Annotations de trouver sa config :

AnnotationConfiguration annCfg = new AnnotationConfiguration();
// sans cette précision, AnnotationConfiguration cherche ses directives dans "/hibernate.properties" au lieu de hibernate.cfg.xml
annCfg.configure("hibernate.cfg.xml");
sessionFactory = annCfg.buildSessionFactory();

Mais comme le chemin est long, tout n'est pas gagné !
voilà t'y pas l'erreur suivante :

org.hibernate.MappingException: Unknown entity: name.cyrille.kitchenonweb.User

Et pourtant j'ai dans hibernate.cfg.xml les lignes suivantes :

<mapping package="name.cyrille.kitchenonweb" />
<mapping class="name.cyrille.kitchenonweb.User"/>

et bien sûr la classe correspondante :

package name.cyrille.kitchenonweb;
import java.io.Serializable;
import javax.persistence.*;
@org.hibernate.annotations.Entity
@org.hibernate.annotations.Table(appliesTo="users")
public class User extends Object implements Serializable {
...
}

Quelle est la solution à cette nouvelle étape ??
...
J'ai trouvé !
...

Embrouillé au début avec le problème des imports, résolu par la création d'une "Persistence Unit", j'ai nommé les tag avec un nom long :

@org.hibernate.annotations.Entity
@org.hibernate.annotations.Table(appliesTo="users")

Et la classe User n'était pas trouvée. Je corrige de cette façon :

@Entity
@Table(name="users")

Et ça fonctionne ! Ouf !
Remarquez l'attribut 'name' de @Table qui était précédemment 'appliesTo'.
En fait il ne s'agissait pas du même Tag.

Quel bordel ce JavaPourLeWeb ... La découverte est fastidieuse ...

Désolé de parler seul ;o)
Merci.
cyrille


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 25, 2006 6:27 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Il y a un bon livre en Français sur Hibernate3
http://www.eyrolles.com/Informatique/Livre/9782212116441/livre-hibernate-3-0.php

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 25, 2006 6:37 pm 
Newbie

Joined: Tue Jul 25, 2006 1:18 pm
Posts: 5
emmanuel wrote:


Yop. J'en ai beaucoup entendu parlé aujourd'hui.
Je vais l'acheter à la rentrée ;o) comme tout le monde dirait-on ;o)

Mais bon, je suis pas sûr qu'il m'aurait donné les infos que j'ai assemblé aujourd'hui. Pour la suite des opérations c'est sûr qu'il va m'aider, mais pour la configuration ... c'est une autre pair de manche dans ce monde qu'est Java ...

merci
cyrille


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.