-2

我已经下载了本教程,并且可以在我的计算机上成功运行它。

一旦我删除了联系人类并添加了我的名为 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.

休眠.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.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;
    }


  }

休眠实用程序

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)
4

1 回答 1

1

乍一看似乎有 2 个问题

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

编辑

解决方案

  1. if (developer != null)在您的“保存(开发人员)”之前放置一个
  2. 重命名Contactcontact
  3. 把hbm文件内容放在问题上,没有那个不能复习映射

希望现在清楚。

编辑 2:

做一个干净的构建。您的构建可能未更新。

于 2013-06-04T05:42:21.577 回答