我的hibernate应用程序在控制台中显示消息,并在浏览器上显示会话关闭错误

时间:2013-06-04 05:30:01

标签: hibernate struts2 hibernate-mapping

我已经下载了这个tutorial,并且可以在我的计算机上成功运行它。

删除联系人类并添加了名为developer的类后,它会显示以下错误。

错误

    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>

的ContactManager

 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

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

ContactAction

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)

1 个答案:

答案 0 :(得分:1)

首先看起来似乎是2个问题的可能性

  1. 您尝试保存的ContactManager对象为空
  2. 您的变量名称'Contact'不正确,它应该是'contact'(一些休眠的东西)
  3. 你的hbm.xml文件在哪里?可能存在“联系”该文件中的类仍然在某处
  4. 的引用

    修改

    <强>解决方案

    1. 在“保存(开发者)”
    2. 之前加if (developer != null)
    3. Contact重命名为contact
    4. 将hbm文件内容放在问题上,而无法查看映射
    5. 希望现在清楚。

      编辑2:

      做一个干净的构建。它的构建可能不会更新。