0

Ich habe versucht, ein einfaches Hibernate 3 Java-Programm zu schreiben - um Daten von DB zu holen, funktioniert es gut, wenn ich es versuche Konfigurieren Sie mit einer hbm.xml Datei, aber wenn ich eine Modellklasse verwenden, um das gleiche zu konfigurieren, habe ich Probleme wie folgt. dhHibernate 3 - MappingException - AnnotationConfiguration Instanz ist erforderlich zu verwenden <mapping class = "xxxxclass" />

<mapping class="example.Pojo_Stock"/> 

statt

<mapping resource="Pojo_Stock.hbm.xml" /> 

Obwohl alle damit verbundenen Anfragen auf Stackoverflow versucht (viele als Duplikat markiert), aber nicht in der Lage, eine Lösung zu finden. Mit freundlicher Hilfe

public class MainApp { 

    private static SessionFactory factory; 
    public static void main(String[] args) { 
     try{ 
      System.out.println("In Main method of MainApp..."); 

      Configuration configure=new Configuration(); 
      configure.configure("hibernate.cfg.xml"); 
      factory = configure.buildSessionFactory(); 

      MainApp mainApp = new MainApp(); 
      mainApp.listDetails_Stock(); 
     } 
     catch(Throwable ex){ 

      System.err.println("Failed to create sessionFactory object." + ex); 
      throw new ExceptionInInitializerError(ex); 

     } 
    } 

    public void listDetails_Stock(){ 
     System.out.println("In listDetails_Stock method of MainApp..."); 

     Session session = factory.openSession(); 
     Transaction tx = null; 

     try{ 
      tx = session.beginTransaction(); 
      List<Pojo_Stock> details = session.createQuery("FROM Pojo_Stock").list(); 
      for(Pojo_Stock detail : details){ 
       System.out.print("\nStockId: " + detail.getStockId()); 
       System.out.print("\nStockCode: " + detail.getStockCode()); 
       System.out.print("\nStockName: " + detail.getStockName()); 
      } 
      tx.commit(); 
     } 


     catch (HibernateException e) { 
      if (tx!=null) tx.rollback(); 
      e.printStackTrace(); 
     }finally { 
      session.close(); 
     } 
    } 

die Modellklasse

@Entity 
@Table(name = "STOCK") 
public class Pojo_Stock { 

    int StockId;  
    String StockCode; 
    String StockName; 
    // Getters & Setters  
} 

und meine Hibernate Config sieht, die wie

sieht

<property name="hibernate.dialect"> 
     org.hibernate.dialect.OracleDialect 
    </property> 
    <property name="hibernate.connection.driver_class"> 
     oracle.jdbc.driver.OracleDriver 
    </property> 

    <property name="hibernate.connection.url"> 
     jdbc:oracle:thin:@xxxx 
    </property> 
    <property name="hibernate.connection.username"> 
     xxxx 
    </property> 
    <property name="hibernate.connection.password"> 
     xxxx 
    </property> 
    <property name="show_sql">true</property> 

    <!-- List of XML mapping files --> 
    <!-- <mapping resource="Pojo_Stock.hbm.xml" /> --> 
    <mapping class="example.Pojo_Stock"/> 
</session-factory> 
wie unten

Fehler, den ich auf meine Konsole ist wie unten,

Failed to create sessionFactory object.org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="example.Pojo_Stock"/> 
Exception in thread "main" java.lang.ExceptionInInitializerError 
    at example.MainApp.main(MainApp.java:28) 
Caused by: org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="example.Pojo_Stock"/> 
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1524) 
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1479) 
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1458) 
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1432) 
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1352) 
    at tutorialspoint.example.MainApp.main(MainApp.java:19) 

Ich versuchte AnnotationConfiguration() statt Configuration() zu verwenden, die nicht stattdessen funktioniert führt den unten Fehler,

Exception in thread "main" java.lang.IncompatibleClassChangeError: org.hibernate.cfg.Mappings 
    at java.lang.ClassLoader.defineClass(ClassLoader.java:262) 
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:69) 
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:540) 
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:451) 
    at java.net.URLClassLoader.access$300(URLClassLoader.java:79) 
    at java.net.URLClassLoader$ClassFinder.run(URLClassLoader.java:1038) 
    at java.security.AccessController.doPrivileged(AccessController.java:314) 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:429) 
    at java.lang.ClassLoader.loadClassHelper(ClassLoader.java:665) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:644) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:358) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:627) 
    at java.lang.J9VMInternals.verifyImpl(Native Method) 
    at java.lang.J9VMInternals.verify(J9VMInternals.java:85) 
    at java.lang.J9VMInternals.initialize(J9VMInternals.java:162) 
    at postgres.hibernate.lessons.MainApp.main(MainApp.java:28) 

Antwort

0

Ich denke, es ist ein Problem mit Ihrem .jar Dateien. Sie verwenden nicht die neueste .jars. Sie versuchen eine annotierende Konfiguration in Ihrem pojo.

@Entity 
@Table(name = "STOCK") 
public class Pojo_Stock { 

    int StockId;  
    String StockCode; 
    String StockName; 
    // Getters & Setters  
} 

Also, zuerst Bitte benutzen Sie AnnotationConfiguration() statt Configuration(); aber Sie haben das schon versucht.

Also, ich denke (möglicherweise) Sie verwenden inkompatible Versionen von Ruhezustand Kern und Anmerkungen. Eine der möglichen Kombinationen ist Hibernate Core 3.3.2 mit Hibernate Annotations 3.4 und versuchen Sie es. Werfen Sie einen Blick auf die Kompatibilitätsmatrixhere für weitere Details.

oder

Sie können auch diese Verbindung Versuchen Hibernate-Error-an-annotationconfiguration-instance-is-required

+0

ich die oben genannten Versionen verwendet haben .. immer noch nicht ... – Sarronya