0

我使用 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);
    }
}

和实体(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 + " ]";
        }

    }

这里有什么问题?

4

1 回答 1

4

您应该使用<mapping class = ... />而不是<mapping resource = ... />

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

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

于 2012-06-25T08:15:18.600 回答