Java 在本地主机上运行时引发SessionFactory创建异常

Java 在本地主机上运行时引发SessionFactory创建异常,java,spring,hibernate,maven,Java,Spring,Hibernate,Maven,我有一个简单的maven项目设置,其中包含一个java类,该类从hibernate.cfg.xml创建一个SessionFactory——当我从主方法运行此应用程序时,它执行得很好: Test.java public static void main(String[] args){ System.out.println("Starting App"); Session session = HibernateUtil.getSessionFactory().openSession(

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

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应用服务器上运行同一个应用程序时,它调用我的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)

请提供您的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>