masT
masT

Reputation: 850

Hibernate mapping exception: Could not get constructor for org.hibernate.entity.SingleTableEntityPersister

The post Hibernate buildSessionFactory() Exception and Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister discusses the similar situation, however I still can't resolve why following code is generating org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister error. I am creating a login, using JSP, Servlet and Hibernate. Please help me resol

Tomcat log:

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

My POJO class for User is:

@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;
    }
}

Corresponding MySQL database table is:

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 (placed in src folder)

<!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>

Please also comment is it beneficial to serialize class. Thanks in advance!

Edit 1: Class, I'm initializing SessionFactory within:

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;
    }
}

Upvotes: 3

Views: 14832

Answers (6)

Piotr Hełminiak
Piotr Hełminiak

Reputation: 51

I encountered this error after upgrading from JDK 8 to JDK 11. In my case, different tricks in pom (with dependencies like javassist) did not help.
The solution was simply to upgrade Hibernate from 5.2.x to 5.4.x (or higher), as from this version suport for JDK 11 is provided out of the box (according to official announcement).

Upvotes: 0

Ali Karimi
Ali Karimi

Reputation: 5

I had this problem I solved by

@Entity @NoArgsConstructor

Instead of

@Entity

Upvotes: 0

Demobilizer
Demobilizer

Reputation: 748

This problem may occur with jdk-11 sometimes.

Following dependency can resolve it.

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

Upvotes: 0

luisbr
luisbr

Reputation: 523

I experienced this issue working with Hibernate version 5.2.17. Switching to 5.3.7 worked fine.

Upvotes: 0

Hector Pasarin
Hector Pasarin

Reputation: 61

I had the same problem, them followed the answer of Vitalii Supryhan and i added:

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

In the pom.xml file, updated the maven project (clicking right button upon the project->maven->update project)

And the problem it was resolved!! Thanks!

Upvotes: 4

Vitalii Supryhan
Vitalii Supryhan

Reputation: 61

Had such problem. The solution was to add dependency: javassist-x.xx.x-y.jar from hibernate distribution

Upvotes: 1

Related Questions