gpt4 book ai didi

Hibernate 映射异常 : Could not get constructor for org. hibernate.entity.SingleTableEntityPersister

转载 作者:行者123 更新时间:2023-12-04 00:41:39 37 4
gpt4 key购买 nike

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

最佳答案

我遇到了同样的问题,他们遵循了 Vitalii Supryhan 的答案我补充说:

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

pom.xml文件,更新了 Maven 项目(在 project->maven->update project 上单击右键)

然后问题就解决了!!谢谢!

关于Hibernate 映射异常 : Could not get constructor for org. hibernate.entity.SingleTableEntityPersister,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18125587/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com