SessionFactory创建在localhost上运行时抛出异常

时间:2015-03-09 16:13:48

标签: java spring hibernate maven

我有一个简单的maven项目设置,其中包含一个从hibernate.cfg.xml创建SessionFactory的java类 - 当我从main方法运行此应用程序时,它执行正常:

Test.java

public static void main(String[] args){
    System.out.println("Starting App");
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    //do some stuff
    //success
}

HibernateUtil.java

private static final SessionFactory sessionFactory;
    static {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            System.out.println("Opening Session Factory...");
            Configuration configuration = new Configuration().configure();
            StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());         
            sessionFactory = configuration.buildSessionFactory(builder.build());
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

然而,当我在Tomcat Application Server上运行这个相同的应用程序时,它调用我的RestController类,它以相同的方式创建SessionFactory:

Controller.java

@RestController
public class MyController {
    MyClass class = null;
    @RequestMapping(value = "/{searchQuery}", produces = "application/json")
    public String search(@PathVariable String searchQuery) throws IOException {
        System.out.println("Starting Session...");
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        //do some stuff
}

返回以下异常:

Starting Session...
Opening Session Factory... //Inside HibernateUtil.java
Mar 09, 2015 11:54:47 AM org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.1
Mar 09, 2015 11:54:47 AM org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
Mar 09, 2015 11:54:47 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
Mar 09, 2015 11:54:47 AM org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
Mar 09, 2015 11:54:48 AM org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
Mar 09, 2015 11:54:48 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
Initial SessionFactory creation failed.org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="com.test.dto.MyClassDTO"/>
Mar 09, 2015 11:54:48 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [MyClass] in context with path [/test] threw exception [Handler processing failed; nested exception is java.lang.ExceptionInInitializerError] with root cause
org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="com.test.dto.MyClassDTO"/>
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1597)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1552)

我认为这个例外具有误导性,因为我不需要实例化AnnotationConfiguration; Configuration工作得很好。在我的应用程序容器上运行时抛出此异常的原因是什么?

编辑:hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
        <!--  Database Connection String -->
        <property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:jtds:sqlserver://localhost:55555/test</property>
        <property name="hibernate.connection.username">user</property>
        <property name="hibernate.connection.password">pass</property>

        <!--  C3p0 Settings -->
        <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
        <property name="hibernate.c3p0.acquire_increment">1</property>
        <property name="hibernate.c3p0.idle_test_period">100</property>
        <property name="hibernate.c3p0.max_size">10</property>
        <property name="hibernate.c3p0.max_statements">10</property>
        <property name="hibernate.c3p0.min_size">10</property>
        <property name="hibernate.c3p0.timeout">100</property>

       <!-- Names the annotated entity class -->
        <mapping class="com.test.dto.MyClassDTO"/>

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

0 个答案:

没有答案