【问题标题】:Hibernate: Failed ManyToOne relationship休眠:多对一关系失败
【发布时间】:2014-02-26 05:59:58
【问题描述】:

我在尝试创建多对一映射时遇到以下错误

Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: org.hibernate.MappingException: Could not determine type for: edu.cs157b.hibernate.Specialty, at table: DOCTOR_INFO, for columns: [org.hibernate.mapping.Column(specialty)]

这是我要映射的两个类

医生

package edu.cs157b.hibernate;

import java.util.ArrayList;

import javax.persistence.*;

@Entity
@Table(name="DOCTOR_INFO")
@NamedQueries (
    {
        @NamedQuery(name = "Doctor.getAll", query = "from Doctor"),
        @NamedQuery(name = "Doctor.findByName", query = "from Doctor where name = :name")
    }
)
public class Doctor implements Person {

    private int id;
    private String name;
    private int appointment_id;

    @ManyToOne (fetch = FetchType.EAGER) 
     @JoinColumn(name="specialty_id") 
    private Specialty specialty; 

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

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

    @Column(unique=true)
    public String getName() {
        return name;
    }

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

    public Specialty getSpecialty() {
        return specialty;
    }

    public void setSpecialty(Specialty specialty) {
        this.specialty = specialty;
    }

    public int getAppointment_id() {
        return appointment_id;
    }

    public void setAppointment_id(int appointment_id) {
        this.appointment_id = appointment_id;
    }
}

专业

package edu.cs157b.hibernate;

import java.util.ArrayList;

import javax.persistence.*;

@Entity
@Table(name="SPECIALTY_INFO")
@NamedQueries (
    {
        @NamedQuery(name = "Specialty.getAll", query = "from Specialty"),
        @NamedQuery(name = "Specialty.findByName", query = "from Specialty where name = :name")
    }
)
public class Specialty {

    private ArrayList<Doctor> doctors;

    private int id;
    private String name;

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    @Column(unique=true)
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public ArrayList<Doctor> getDoctors() {
        return doctors;
    }
    public void setDoctors(ArrayList<Doctor> doctors) {
        this.doctors = doctors;
    }    

}

【问题讨论】:

    标签: java hibernate jpa associations


    【解决方案1】:

    Specialty 中的getDoctors() 应使用@OneToMany(mappedBy = "specialty") 注释

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多