Hibernate和MappingNotFoundException

时间:2012-06-25 07:42:22

标签: java xml hibernate mapping

我使用的是Netbeans IDE。

我的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>
    //properties

    <mapping resource="entities.Employee"/>
    <mapping resource="entities.Human"/>
  </session-factory>
</hibernate-configuration>

hibernate.cfg.xml在

NetBeansProjects\HibernateTest\build\web\WEB-INF\classes\

初始化SessionFactory的代码如下所示:

static {
    try { 
        String configFilePath = "/hibernate.cfg.xml";
        URL configFileURL = SessionFactory.class.getResource(configFilePath);
        sessionFactory = new Configuration().configure(configFileURL).buildSessionFactory();
    } catch (Throwable ex) {
        // Log the exception. 
        throw new RuntimeException(ex);
    }
}

和entites(Employee.class和Human.class)在

\NetBeansProjects\HibernateTest\build\web\WEB-INF\classes\entities\

班级员工:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import javax.persistence.*;

@Entity
@Table(name = "EMPLOYEE")
public class Employee implements Serializable {

    public Employee(){
    }

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    @Column(name = "EMPLOYEE_ID")
    private int employeeId;

    @Column(name = "SALARY")
    private double salary;

    @OneToOne(cascade = CascadeType.ALL, fetch= FetchType.LAZY)
    @PrimaryKeyJoinColumn
    private Human humDate;

    public int getEmployeeId() {
        return employeeId;
    }

    public void setEmployeeId(int employeeId) {
        this.employeeId = employeeId;
    }

    public Human getHumDate() {
        return humDate;
    }

    public void setHumDate(Human humDate) {
        this.humDate = humDate;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

}

类人类:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

    package entities;

    import java.io.Serializable;
    import javax.persistence.*;

    @Entity
    @Table(name = "HUMAN")
    public class Human implements Serializable {

        public Human(){
        }

        public Human(String name){
            this.name = name;
        }

        private static final long serialVersionUID = 1L;
        @Id
        @GeneratedValue(strategy = GenerationType.TABLE)
        private int id;

        @Column(name = "HUM_NAME")
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        @Override
        public int hashCode() {
            int hash = 0;
            hash += (int) id;
            return hash;
        }

        @Override
        public boolean equals(Object object) {
            // TODO: Warning - this method won't work in the case the id fields are not set
            if (!(object instanceof Human)) {
                return false;
            }
            Human other = (Human) object;
            if (this.id != other.id) {
                return false;
            }
            return true;
        }

        @Override
        public String toString() {
            return "com.nsn.Human[ id=" + id + " ]";
        }

    }

这里有什么问题?

1 个答案:

答案 0 :(得分:4)

您应该使用<mapping class = ... />代替<mapping resource = ... />

<mapping class = "entities.Employee" />
<mapping class = "entities.Human" /> 

<mapping resource = ... />用于映射.hbm.xml文件而不是注释。