Hibernate映射异常:无法获取org.hibernate.entity.SingleTableEntityPersister的构造函数

时间:2013-08-08 12:05:08

标签: hibernate java-ee hibernate-annotations

帖子Hibernate buildSessionFactory() ExceptionCould not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister讨论了类似的情况,但是我仍然无法解决为什么以下代码生成 org.hibernate.MappingException:无法获取org.hibernate的构造函数.persister.entity.SingleTableEntityPersister 错误。我正在使用JSP,Servlet和Hibernate创建一个登录。请帮我解决

Tomcat日志:

Aug 08, 2013 5:07:58 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\DMIX;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files\TortoiseSVN\bin;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\;.
Aug 08, 2013 5:07:58 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Assignment5' did not find a matching property.
Aug 08, 2013 5:07:58 PM org.apache.coyote.AbstractProtocolHandler init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Aug 08, 2013 5:07:58 PM org.apache.coyote.AbstractProtocolHandler init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Aug 08, 2013 5:07:58 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 479 ms
Aug 08, 2013 5:07:58 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Aug 08, 2013 5:07:58 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.12
Aug 08, 2013 5:08:00 PM org.apache.coyote.AbstractProtocolHandler start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Aug 08, 2013 5:08:00 PM org.apache.coyote.AbstractProtocolHandler start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Aug 08, 2013 5:08:00 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1186 ms
Aug 08, 2013 5:08:04 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
Aug 08, 2013 5:08:04 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.3.Final}
Aug 08, 2013 5:08:04 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000205: Loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=org.h2.Driver, hibernate.dialect=org.hibernate.dialect.H2Dialect, hibernate.max_fetch_depth=5, hibernate.format_sql=true, hibernate.generate_statistics=true, hibernate.connection.username=sa, hibernate.connection.url=jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE, hibernate.bytecode.use_reflection_optimizer=false, hibernate.jdbc.batch_versioned_data=true, hibernate.connection.pool_size=5}
Aug 08, 2013 5:08:04 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Aug 08, 2013 5:08:04 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Aug 08, 2013 5:08:04 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Aug 08, 2013 5:08:04 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Aug 08, 2013 5:08:04 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Aug 08, 2013 5:08:04 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Aug 08, 2013 5:08:04 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 5
Aug 08, 2013 5:08:04 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
Aug 08, 2013 5:08:04 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/Assignment5]
Aug 08, 2013 5:08:04 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=root, password=****}
Aug 08, 2013 5:08:05 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLInnoDBDialect
Aug 08, 2013 5:08:05 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
Aug 08, 2013 5:08:05 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Aug 08, 2013 5:08:05 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Initializing session factory failed: org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister
java.lang.ExceptionInInitializerError

我的用户POJO课程是:

@Entity
@Table(name = "Users")
public class User implements Serializable {

    private static final long serialVersionUID = 1986328586398921108L;

    @Id
    @GeneratedValue
    @Column(name="ID")
    private Long id;

    @NotNull
    @Column(name="NAME", unique = true)
    private String name;


    @NotNull
    @Column(name="PASSWORD")
    private String password;

    public User() {}

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

对应的MySQL数据库表是:

create table Users (
    ID bigint not null auto_increment primary key,
    NAME varchar(20) not null unique,
    PASSWORD varchar(20) not null
);

hibernate.cfg.xml(放在src文件夹中)

<!DOCTYPE hibernate-configuration
    PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>

        <!-- hibernate dialect -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>

        <!-- Logging all generated SQL to console -->
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>

        <!-- Database connection settings: MySQL -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/RnD</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">*******</property>
    <!--<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property> -->

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>


        <!-- Name the annotated User class -->
        <mapping class = "pojo.User"/>

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

请注释是否有利于序列化类。提前谢谢!

编辑1: 类,我正在初始化SessionFactory:

public class HibernateUtil {

    private static final SessionFactory sessionFactory;
    static {
        try {
            Configuration configuration = new Configuration();
            configuration.configure();
            ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
                                                .applySettings(configuration.getProperties())
                                                .buildServiceRegistry();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        } catch (Throwable e) {
            System.err.println("Initializing session factory failed: " + e);
            throw new ExceptionInInitializerError(e);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

4 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,他们按照Vitalii Supryhan的回答加入了我的话:

<dependency>
    <groupId>javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.12.1.GA</version>
</dependency>

pom.xml文件中,更新了maven项目(点击project->maven->update project上的右键)

问题解决了!!谢谢!

答案 1 :(得分:1)

有这样的问题。解决方案是添加依赖: 了Javassist-x.xx.x-y.jar 来自hibernate发行版

答案 2 :(得分:0)

我在使用Hibernate 5.2.17版本时遇到了这个问题。切换到5.3.7效果很好。

答案 3 :(得分:0)

jdk-11 有时可能会出现这个问题。

以下依赖可以解决。

<dependency>
    <groupId>org.javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.23.1-GA</version>
</dependency>