我的hibernate应用程序在控制台中显示消息,并在浏览器上显示会话关闭错误
我下载了这个,可以在我的电脑上成功运行 一旦我删除了contact类并添加了名为developer的类,它就会显示以下错误 错误我的hibernate应用程序在控制台中显示消息,并在浏览器上显示会话关闭错误,hibernate,struts2,hibernate-mapping,Hibernate,Struts2,Hibernate Mapping,我下载了这个,可以在我的电脑上成功运行 一旦我删除了contact类并添加了名为developer的类,它就会显示以下错误 错误 type Exception report message descriptionThe server encountered an internal error () that prevented it from fulfilling this request. exception javax.servlet.ServletException: PW
type Exception report
message
descriptionThe server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: PWC1243: Filter execution threw an exception
root cause
java.lang.NoClassDefFoundError: Lnet/viralpatel/contact/model/Contact;
root cause
java.lang.ClassNotFoundException: net.viralpatel.contact.model.Contact
note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.1.2.2 logs.
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="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/test
</property>
<property name="connection.username">root</property>
<property name="connection.password"></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>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">
org.hibernate.cache.NoCacheProvider
</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping class="net.viralpatel.contact.model.Developer" />
</session-factory>
</hibernate-configuration>
package net.viralpatel.contact.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
System.out.println("********************************in session");
return new AnnotationConfiguration().configure()
.buildSessionFactory();
} catch (Throwable ex) {
System.out.println("this exception");
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
开发者
package net.viralpatel.contact.controller;
import org.hibernate.classic.Session;
import net.viralpatel.contact.model.Developer;
import net.viralpatel.contact.util.HibernateUtil;
public class ContactManager extends HibernateUtil {
public void add(Developer developer) {
System.err.println("Here is add");
//Session session1 = HibernateUtil.getSessionFactory().getCurrentSession();
Session session1 = HibernateUtil.getSessionFactory().openSession();
session1.beginTransaction();
session1.save(developer);
session1.getTransaction().commit();
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package net.viralpatel.contact.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*
* @author Jack Ramzi
*/
@Entity
@Table(name="developer")
public class Developer implements Serializable {
private static final long serialVersionUID = -8767337896773261247L;
private int id;
private String contact;
private int phone_1;
@Id
@GeneratedValue
@Column(name="id")
public int getID() {
return id;
}
public void setID(int ID) {
this.id = ID;
}
@Column(name="contact")
public String getContact() {
return contact;
}
public void setContact(String Contact) {
this.contact = Contact;
}
@Column(name="phone_1")
public int getPhone_1() {
return phone_1;
}
public void setPhone_1(int Phone_1) {
this.phone_1 = Phone_1;
}
}
HibernateUtil
<?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="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/test
</property>
<property name="connection.username">root</property>
<property name="connection.password"></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>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">
org.hibernate.cache.NoCacheProvider
</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping class="net.viralpatel.contact.model.Developer" />
</session-factory>
</hibernate-configuration>
package net.viralpatel.contact.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
System.out.println("********************************in session");
return new AnnotationConfiguration().configure()
.buildSessionFactory();
} catch (Throwable ex) {
System.out.println("this exception");
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
联系行动
package net.viralpatel.contact.view;
import net.viralpatel.contact.controller.ContactManager;
import com.opensymphony.xwork2.ActionSupport;
import net.viralpatel.contact.model.Developer;
public class ContactAction extends ActionSupport {
private static final long serialVersionUID = 9149826260758390091L;
private Developer developer;
private ContactManager linkController;
public ContactAction() {
linkController = new ContactManager();
}
public String execute() {
return SUCCESS;
}
public String add() {
try{
System.out.println("in add of contactAction");
developer = new Developer();
developer.setID(1);
developer.setContact("jack");
developer.setPhone_1(123344);
linkController.add(developer);}
catch(Exception e){
System.out.println("this exception 2");
e.printStackTrace();
}
return SUCCESS;
}
public Developer getDeveloper() {
return developer;
}
public void setDeveloper(Developer developer) {
this.developer = developer;
}
public ContactManager getLinkController() {
return linkController;
}
public void setLinkController(ContactManager linkController) {
this.linkController = linkController;
}
}
开发者表
id int(11)
contact varchar(15)
phone_1 int(15)
乍一看,似乎有两个问题的可能性
if(developer!=null)
联系人
重命名为联系人
干净利落。可能您的构建没有更新。谢谢,我从stacktrace中注意到了这些问题,但不知道如何解决这些问题。我发布了hbm.xml,其中没有联系人的痕迹。我使用的是映射注释,而不是xml文件。哦,是的,很抱歉没有注意到。很高兴知道我的建议有所帮助。我已经,如果hbm文件也在那里。没有这一点,这是一个不完整的问题(我肯定不值得投反对票)。