Java 初始会话工厂创建失败。org.hibernate.AnnotationException:@OneToOne或@ManyToOne on net.viralpatel.hibernate.Employee.department

Java 初始会话工厂创建失败。org.hibernate.AnnotationException:@OneToOne或@ManyToOne on net.viralpatel.hibernate.Employee.department,java,hibernate,Java,Hibernate,我不熟悉hibernate。我开发了OneToMany关系示例,但我看到了下面的示例请帮助我解决以下错误? INFO: Bind entity net.viralpatel.hibernate.Employee on table EMPLOYEE Initial SessionFactory creation failed.org.hibernate.AnnotationException: @OneToOne or @ManyToOne on net.viralpatel.hibernate.

我不熟悉
hibernate
。我开发了
OneToMany
关系示例,但我看到了下面的示例请帮助我解决以下错误?

INFO: Bind entity net.viralpatel.hibernate.Employee on table EMPLOYEE
Initial SessionFactory creation failed.org.hibernate.AnnotationException: @OneToOne or @ManyToOne on net.viralpatel.hibernate.Employee.department references an unknown entity: net.viralpatel.hibernate.Department
Exception in thread "main" java.lang.ExceptionInInitializerError
    at net.viralpatel.hibernate.HibernateUtil.buildSessionFactory(HibernateUtil.java:18)
    at net.viralpatel.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:8)
    at net.viralpatel.hibernate.Main.main(Main.java:12)
Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on net.viralpatel.hibernate.Employee.department references an unknown entity: net.viralpatel.hibernate.Department
    at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:81)
    at org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:499)
    at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:304)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)
    at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)
    at net.viralpatel.hibernate.HibernateUtil.buildSessionFactory(HibernateUtil.java:15)
    ... 2 more

public class Department {
    @Id
    @GeneratedValue
    @Column(name="DEPARTMENT_ID")
    private Long departmentId;
    @Column(name="DEPT_NAME")
    private String departmentName;
    @OneToMany
    private List<Employee> employees;

    public Long getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentId(Long departmentId) {
        this.departmentId = departmentId;
    }

    public String getDepartmentName() {
        return departmentName;
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }

    public List<Employee> getEmployees() {
        return employees;
    }

    public void setEmployees(List<Employee> employees) {
        this.employees = employees;
    }
}
主要

公共类主{
公共静态void main(字符串[]args){
SessionFactory sf=HibernateUtil.getSessionFactory();
Session Session=sf.openSession();
session.beginTransaction();
部门=新部门();
部门名称设置部门名称(“销售”);
员工emp1=新员工(“尼娜”、“迈尔斯”、“111”);
员工emp2=新员工(“托尼”、“阿尔梅达”、“222”);
department.setEmployees(新ArrayList());
department.getEmployees().add(emp1);
department.getEmployees().add(emp2);
session.save(部门);
session.getTransaction().commit();
session.close();
}
}

使用@Entity注释将部门类标记为实体

使用@Entity注释将部门类标记为实体

您是否用
@Entity
注释了
部门
?是否用
@Entity
注释了
部门
public class Department {
    @Id
    @GeneratedValue
    @Column(name="DEPARTMENT_ID")
    private Long departmentId;
    @Column(name="DEPT_NAME")
    private String departmentName;
    @OneToMany
    private List<Employee> employees;

    public Long getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentId(Long departmentId) {
        this.departmentId = departmentId;
    }

    public String getDepartmentName() {
        return departmentName;
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }

    public List<Employee> getEmployees() {
        return employees;
    }

    public void setEmployees(List<Employee> employees) {
        this.employees = employees;
    }
}
public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}
public class Main {

    public static void main(String[] args) {

        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session session = sf.openSession();
        session.beginTransaction();

        Department department = new Department();
        department.setDepartmentName("Sales");

        Employee emp1 = new Employee("Nina", "Mayers", "111");
        Employee emp2 = new Employee("Tony", "Almeida", "222");

        department.setEmployees(new ArrayList<Employee>());
        department.getEmployees().add(emp1);
        department.getEmployees().add(emp2);

        session.save(department);

        session.getTransaction().commit();
        session.close();
    }
}