Ruhezustand - Zur Verwendung ist eine AnnotationConfiguration-Instanz erforderlich

Ruhezustand - Eine AnnotationConfiguration-Instanz muss verwendet werden

Für die Erstellung der Sitzungsfactory ist die Annotation "AnnotationConfiguration" anstelle der normalen "Configuration ()" für den Ruhezustand erforderlich.

INFO: Configuration resource: /hibernate.cfg.xml
Initial SessionFactory creation failed.org.hibernate.MappingException:
An AnnotationConfiguration instance is required to use 
Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.example.persistence.HibernateUtil.buildSessionFactory(HibernateUtil.java:19)
    at com.example.persistence.HibernateUtil.(HibernateUtil.java:8)
    at com.example.common.App.main(App.java:11)
Caused by: org.hibernate.MappingException: An AnnotationConfiguration instance is required to use 
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1600)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1428)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1414)
    at com.example.persistence.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
    ... 2 more

Lösung

1. Laden Sie die Anmerkungsbibliothek für den Ruhezustand herunter

Sie können die Bibliothek vonHibernate official website herunterladen

Or

Fügen Sie die Abhängigkeit in Mavens pom.xml hinzu

        
    
        hibernate-annotations
        hibernate-annotations
        3.3.0.GA
    

P.S You may need to include the JBoss repository in order to download the Hibernate annotation library.


    
      JBoss repository
      http://repository.jboss.com/maven2/
    
  

2. Verwenden Sie AnnotationConfiguration, um die Session Factory zu erstellen

Die normale XML-Dateizuordnung im Ruhezustand verwendet Configuration ().

          return new Configuration().configure().buildSessionFactory();

Für die Annotation im Ruhezustand müssen Sie sie in "AnnotationConfiguration" ändern.

          return new AnnotationConfiguration().configure().buildSessionFactory();
HibernateUtil.java

Ein vollständiges Beispiel für "HibernateUtil.java" für die Verwendung von "AnnotationConfiguration" für die Annotationsanwendung im Ruhezustand.

package com.example.persistence;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new AnnotationConfiguration().configure().buildSessionFactory();

        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void shutdown() {
        // Close caches and connection pools
        getSessionFactory().close();
    }

}