帖子Hibernate buildSessionFactory() Exception和Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister讨论了类似的情况,但是我仍然不能解决为什么下面的代码会生成org. hib.MappingException:无法获取org. hib.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;
}
}
6条答案
按热度按时间czq61nw11#
我有同样的问题,他们遵循Vitalii Supryhan的答案,我补充说:
在
pom.xml
文件中,更新了maven项目(在project->maven->update project
上单击右键)而且问题解决了!!谢谢!
dl5txlt92#
有这样的问题。解决办法是添加依赖关系:* javassist-x.xx.x-y.jar *(来自Hibernate发行版)
vaqhlq813#
我在使用Hibernate版本5. 2. 17时遇到了这个问题。切换到5. 3. 7工作正常。
2skhul334#
jdk-11有时可能会出现此问题。
以下依赖项可以解决此问题。
ki1q1bka5#
我解决了这个问题
@实体@NoArgsConstructor
代替
@实体
q3qa4bjr6#
从JDK 8升级到JDK 11后我遇到了这个错误,在我的情况下,pom中的不同技巧(带有像javassist这样的依赖关系)没有帮助。
解决方案只是将Hibernate从5.2.x升级到5.4.x(或更高版本),因为从此版本开始,JDK 11支持是开箱即用的(根据官方announcement)。