Hibernate 3 - MappingException - 使用<mapping class =“xxxxclass”>

时间:2016-04-19 07:18:41

标签: java hibernate configuration annotations configuration-files

我尝试编写一个简单的Hibernate 3 java程序 - 从DB中获取数据,当我尝试使用hbm.xml文件进行配置但是当我使用模型类来配置相同时,它工作得很好,我面临如下问题。 即

<mapping class="example.Pojo_Stock"/>  

而不是

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

虽然尝试了stackoverflow上的所有相关查询(许多标记为重复)但无法找到解决方案。请帮助

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(); 
        }
    }

模型类看起来像

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

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

我的hibernate配置如下所示,

    

    <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>

我在控制台上遇到的错误如下,

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)

我尝试使用AnnotationConfiguration()代替Configuration()但不起作用而是抛出以下错误,

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)

1 个答案:

答案 0 :(得分:0)

我认为您的.jar文件存在问题。您没有使用最新的.jars。您正在pojo尝试注释配置。

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

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

所以,首先请使用AnnotationConfiguration()代替Configuration(); 但你已经尝试过了。

所以,我认为(可能)你正在使用不兼容的hibernate核心版本和注释。其中一个可能的组合是 Hibernate Core 3.3.2 with Hibernate Annotations 3.4 并尝试。有关更多详细信息,请查看兼容性矩阵 here

您也可以尝试此链接Hibernate-Error-an-annotationconfiguration-instance-is-required