您好我是Hibernate的新手,我正在尝试了解它是如何工作的,所以我创建了一个servlet,它通过hibernate与DB建立连接。一切正常,直到我将一个hbm文件添加到configuration.cfg.xml。我无法理解为什么当我添加这个servlet崩溃时。
我的目录是:
hibernate.cfg.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
<property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
<property name="hibernate.connection.url">jdbc:derby://localhost:3306/realestate</property>
<property name="hibernate.connection.username">test</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</property>
<!-- JDBC connection pool (use the built-in) -->
<property name='connection.pool_size'>1</property>
<!-- SQL dialect -->
<property name='dialect'>org.hibernate.dialect.MySQLDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name='show_sql'>true</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<--without the below line works fine-->
<mapping resource="Entities/Users.hbm.xml"/>
</session-factory>
</hibernate-configuration>
用户表:
CREATE TABLE IF NOT EXISTS `realEstate`.`Users` (
`user_id` INT NOT NULL AUTO_INCREMENT,
`Username` VARCHAR(45) NULL,
`Password` VARCHAR(45) NULL,
`RegistrationDate` VARCHAR(15) NULL,
`Name` VARCHAR(45) NULL,
`Surname` VARCHAR(45) NULL,
`Phone` VARCHAR(45) NULL,
`Email` VARCHAR(45) NULL,
`Registered` TINYINT(1) NULL,
PRIMARY KEY (`user_id`),
UNIQUE INDEX `user_id_UNIQUE` (`user_id` ASC))
ENGINE = InnoDB;
用户类
package Entities;
public class Users {
private int user_id;
private String username;
private String password;
private String date;
private String name;
private String surname;
private String phone;
private String email;
private boolean registered;
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public boolean getRegistered() {
return registered;
}
public void setRegistered(boolean registered) {
this.registered = registered;
}
}
和hbm文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="entities.Users" table="USERS">
<id name="user_id" type="int">
<column name="user_id" />
<generator class="assigned" />
</id>
<property name="username" type="java.lang.String">
<column name="Username" length="45"/>
</property>
<property name="password" type="java.lang.String">
<column name="Password" length="45" />
</property>
<property name="name" type="java.lang.String">
<column name="Name" length="45"/>
</property>
<property name="date" type="java.lang.String">
<column name="RegistrationDate" length="45"/>
</property>
<property name="surname" type="java.lang.String">
<column name="Surname" length="45"/>
</property>
<property name="phone" type="java.lang.String">
<column name="Phone" length="45"/>
</property>
<property name="email" type="java.lang.String">
<column name="Email" length="45"/>
</property>
<property name="registered" type="boolean">
<column name="Registered" />
</property>
</class>
</hibernate-mapping>
我尝试添加另一个hbm文件但问题仍然存在。
你能帮帮我吗? Thx提前!编辑:这是堆栈跟踪 类型异常报告
message错误实例化servlet类servlets.a
description服务器遇到阻止它的内部错误 完成此请求。
例外
javax.servlet.ServletException:实例化servlet类时出错 servlets.a org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526) org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078) org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:655) org.apache.coyote.http11.Http11NioProtocol $ Http11ConnectionHandler.process(Http11NioProtocol.java:222) org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1566) org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1523) java.util.concurrent.ThreadPoolExecutor.runWorker(未知来源) java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知来源) org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61) java.lang.Thread.run(未知来源)
根本原因
java.lang.ExceptionInInitializerError hibernate.HibernateUtil。(HibernateUtil.java:22) servlets.a。(a.java:35) sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法) sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) sun.reflect.DelegatingConstructorAccessorImpl.newInstance(未知 来源)java.lang.reflect.Constructor.newInstance(未知来源) java.lang.Class.newInstance(未知来源) org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526) org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078) org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:655) org.apache.coyote.http11.Http11NioProtocol $ Http11ConnectionHandler.process(Http11NioProtocol.java:222) org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1566) org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1523) java.util.concurrent.ThreadPoolExecutor.runWorker(未知来源) java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知来源) org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61) java.lang.Thread.run(未知来源)
根本原因
java.lang.VerifyError:class org.hibernate.type.WrappedMaterializedBlobType重写final方法 getReturnedClass()Ljava /郎/类。 java.lang.ClassLoader.defineClass1(本机方法) java.lang.ClassLoader.defineClass(未知来源) java.security.SecureClassLoader.defineClass(未知来源) org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2479) org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:880) org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1294) org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1177) org.hibernate.type.BasicTypeRegistry。(BasicTypeRegistry.java:91) org.hibernate.type.TypeResolver。(TypeResolver.java:59) org.hibernate.cfg.Configuration。(Configuration.java:250) org.hibernate.cfg.Configuration。(Configuration.java:302) org.hibernate.cfg.AnnotationConfiguration。(AnnotationConfiguration.java:168) hibernate.HibernateUtil。(HibernateUtil.java:15) servlets.a。(a.java:35) sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法) sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) sun.reflect.DelegatingConstructorAccessorImpl.newInstance(未知 来源)java.lang.reflect.Constructor.newInstance(未知来源) java.lang.Class.newInstance(未知来源) org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526) org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078) org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:655) org.apache.coyote.http11.Http11NioProtocol $ Http11ConnectionHandler.process(Http11NioProtocol.java:222) org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1566) org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1523) java.util.concurrent.ThreadPoolExecutor.runWorker(未知来源) java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知来源) org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61) java.lang.Thread.run(未知来源)
注意Apache中提供了根本原因的完整堆栈跟踪 Tomcat / 8。0。9日志。
答案 0 :(得分:0)
您的班级名称应为&#34; Entities.Users&#34;而不是&#34; entities.User&#34;在您的映射文件中。我宁愿和&#34;用户&#34;并指定像<hibernate-mapping package="Entities">
这样的包
但我非常怀疑这是否会导致问题。你能否提供错误的堆栈跟踪?