Error creating bean with name 'sessionFactory&

2020-04-17 04:39发布

问题:

I am developing an application with Hibernate and spring and apach tomcate

The complete stacktrace is:

GRAVE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/HibernateSessionFactory.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1403)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:545)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:871)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:443)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:459)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:340)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:307)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
at javax.servlet.GenericServlet.init(GenericServlet.java:160)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:110)
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructDefaultTuplizer(EntityTuplizerFactory.java:135)
at org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.<init>(EntityEntityModeToTuplizerMapping.java:56)
at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:323)
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:433)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:109)
at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:231)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1313)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:855)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:774)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1460)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1400)
... 28 more

My Web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<description>Sample application</description>
<display-name>RichFaces 4</display-name>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>

My spring-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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: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/context


http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan
    base-package="net.viralpatel.spring3.controller" />

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/site/" />
    <property name="suffix" value=".jsp" />
</bean>

<import resource="DataSource.xml"/>
<import resource="HibernateSessionFactory.xml"/>
<bean id="hibernatetransactionManager"

    class="org.springframework.orm.hibernate3.HibernateTransactionManager">

    <property name="sessionFactory" ref="sessionFactory" />

</bean>   
</beans>

My HibernateSessionFactory.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="sessionFactory" 
 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="dataSource">
  <ref bean="dataSource"/>
</property>

<property name="hibernateProperties">
   <props>
     <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
     <prop key="hibernate.show_sql">true</prop>
     <prop key="hibernate.hbm2ddl.auto">update</prop>
   </props>
</property>

<property name="mappingResources">
<list>
      <value>Utilisateur.hbm.xml</value>

</list>
 </property>    

</bean>
</beans>

My Utilisateur.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 7 nov. 2013 17:56:05 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping package="model">
<class name="Utilisateur" table="UTILISATEUR">
    <id name="id" type="int">
        <column name="ID" />
        <generator class="assigned" />
    </id>
    <property name="nom" type="java.lang.String">
        <column name="NOM" />
    </property>
    <property name="prenom" type="java.lang.String">
        <column name="PRENOM" />
    </property>
    <property name="code" type="java.lang.String">
        <column name="CODE" />
    </property>
    <property name="sex" type="java.lang.String">
        <column name="SEX" />
    </property>
    <property name="telephone" type="java.lang.String">
        <column name="TELEPHONE" />
    </property>
    <property name="login" type="java.lang.String">
        <column name="LOGIN" />
    </property>
    <property name="password" type="java.lang.String">
        <column name="PASSWORD" />
    </property>

</class>
</hibernate-mapping>

My Utilisateur.java

package model;

import java.util.Date;
import java.util.List;

public class Utilisateur {
private Integer id;
private String nom;
private String prenom;
private String code;
private String sex;
private String telephone;
private String login;
private String password;
public Utilisateur() {

}
public Integer getId() {
    return id;
}
public void setId(Integer id) {
    this.id = id;
}
public String getSex() {
    return sex;
}
public void setSex(String sex) {
    this.sex = sex;
}
public String getLogin() {
    return login;
}
public void setLogin(String login) {
    this.login = login;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
public void setPrenom(String prenom) {
    this.prenom = prenom;
}
public String getPrenom() {
    return prenom;
}
public void setCode(String code) {
    this.code = code;
}
public String getCode() {
    return code;
}
public void setTelephone(String telephone) {
    this.telephone = telephone;
}
public String getTelephone() {
    return telephone;
}

/**
 * @param nom the nom to set
 */
public void setNom(String nom) {
    this.nom = nom;
}
/**
 * @return the nom
 */
public String getNom() {
    return nom;
}   
}

thank you

回答1:

compare this file Utilisateur.java with Utilisateur.hbm.xml.

  • check attribute names are same in two file
  • include one default constructor in Utilisateur.java
  • you may miss this jar file. javassist.jar

when you encounter this error Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]. it is a generic message. So you have to look at the last line that caused the error. that could be solution.



回答2:

I had the same issue about an hour ago and solved it using the following tips mentioned in the link

http://jonathan.lalou.free.fr/?p=2026

The problem is because of asm jar incompatibility with hibernate version. So as soon as you change it the problem will be solved.

make sure to add the property tags out of the dependencies. Also clean your project to remove everything from target folder. Then clean install your maven to retrieve all compatible jars.