AnnotationConfiguration实例是必需的

时间:2012-07-03 08:59:12

标签: java hibernate spring-mvc

我是春天和休眠的新人。现在我正在尝试将一些数据保存到mySql数据库。但是我收到了一些错误。 dispatcher-servelet.xml中是否需要使用hibernate注释进行任何配置。我该如何解决这个问题呢?

org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.ExceptionInInitializerError
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:839)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:669)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:585)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)


root cause 
java.lang.ExceptionInInitializerError
com.application.util.HibernateUtil.<clinit>(HibernateUtil.java:21)
com.application.service.EmployeeServiceImpl.add(EmployeeServiceImpl.java:22)
com.application.controller.UserController.onSubmit(UserController.java:34)
org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:415)
org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:387)
org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:272)
org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:268)
org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:669)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:585)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)


root cause 
org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="com.application.domain.Employee"/>
org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1600)
org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555)
org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534)
org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508)
org.hibernate.cfg.Configuration.configure(Configuration.java:1428)
org.hibernate.cfg.Configuration.configure(Configuration.java:1414)
com.application.util.HibernateUtil.<clinit>(HibernateUtil.java:18)
com.application.service.EmployeeServiceImpl.add(EmployeeServiceImpl.java:22)
com.application.controller.UserController.onSubmit(UserController.java:34)
org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:415)
org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:387)
org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:272)
org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:268)
org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:669)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:585)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

Hibernate配置文件是

的hibernate.cfg.xml

<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/employee_db</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password"></property>
    <property name="connection.pool_size">1</property>
    <property name="show_sql">true</property>
    <property name="hbm2ddl.auto">create</property>

    <mapping class="com.application.domain.Employee" />
  </session-factory>
</hibernate-configuration>

调度员servelet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:p="http://www.springframework.org/schema/p"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="index.htm">indexController</prop>
        </props>
    </property>
</bean>

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />

<bean name="indexController"
      class="org.springframework.web.servlet.mvc.ParameterizableViewController"
      p:viewName="index" />

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" 
p:basename="messages" />
<bean id="employeeService" class="com.application.service.EmployeeServiceImpl" />
<bean id="employeeValidator" class="com.application.validator.EmployeeValidator" />
<bean name="userRegistration.htm" class="com.application.controller.UserController" 
    p:employeeService-ref="employeeService" p:formView="employeeForm" 
    p:successView="employeeSuccess" p:validator-ref="employeeValidator"/>  

</beans>

使用hibernet.cfg.xml后回答 新例外1

org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class com.application.util.HibernateUtil
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:839)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:669)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:585)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)


root cause 
java.lang.NoClassDefFoundError: Could not initialize class com.application.util.HibernateUtil
com.application.service.EmployeeServiceImpl.add(EmployeeServiceImpl.java:22)
com.application.controller.UserController.onSubmit(UserController.java:34)
org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:415)
org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:387)
org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:272)
org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:268)
org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:669)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:585)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

这门课有什么问题 EmployeeServiceImpl.java和HibernateUtil.java

public class EmployeeServiceImpl implements EmployeeService {

@Override
public void add(Employee employee) {
//Persist the user object here. 
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = null;
    try {
        transaction = session.beginTransaction();
        session.save(employee);
        transaction.commit();
    } catch (HibernateException e) {
        transaction.rollback();
    } finally {
        session.close();
    }
    System.out.println("User added successfully");
}
}

public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
    try {
        sessionFactory = new Configuration().configure().buildSessionFactory();
    } catch (Throwable ex) {
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
} 

2 个答案:

答案 0 :(得分:0)

需要添加hibernate-annotations jar并将sessionFactory构建为

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

Take a look

答案 1 :(得分:-1)

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="/WEB-INF/hibernate.cfg.xml"></property>
        <property name="configurationClass">            
            <value>org.hibernate.cfg.AnnotationConfiguration</value>        
        </property>
        <property name="hibernateProperties">
       <props>
           <prop key="hibernate.dialect">${jdbc.dialect}</prop>
           <prop key="hibernate.show_sql">${jdbc.showsql}</prop>
       </props>
   </property>

你可以使用上面的配置,对我来说很好。