Hibernate查询目标关系表

时间:2016-07-20 13:14:52

标签: java hibernate hibernate-criteria

我正在使用Hibernate来建模由关系表连接的2个表,内部变量如下:

// Clinic.java

@Entity
@Table(name = "clinic")
public class Clinic
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private long id;

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

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.clinic", cascade = CascadeType.ALL)
    private Set<ClinicDoctor> clinicDoctors = new HashSet<ClinicDoctor>(0);

    public long getId()
    {
        return id;
    }

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

    public String getName()
    {
        return name;
    }

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

    @JsonIgnore
    public Set<ClinicDoctor> getClinicDoctors()
    {
        return clinicDoctors;
    }

    public void setClinicDoctors(Set<ClinicDoctor> clinicDoctors)
    {
        this.clinicDoctors = clinicDoctors;
    }
}

// Doctor.java

@Entity
@Table(name = "doctor")
public class Doctor
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private long id;

    @Column(name = "name", nullable = false)
    private String name;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.doctor", cascade = CascadeType.ALL)
    private Set<ClinicDoctor> clinicDoctors = new HashSet<ClinicDoctor>(0);

    public long getId()
    {
        return id;
    }

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

    public String getName()
    {
        return name;
    }

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

    @JsonIgnore
    public Set<ClinicDoctor> getClinicDoctors()
    {
        return clinicDoctors;
    }

    public void setClinicDoctors(Set<ClinicDoctor> clinicDoctors)
    {
        this.clinicDoctors = clinicDoctors;
    }
}

// ClinicDoctor

@Entity
@Table(name = "clinic_doctor")
@AssociationOverrides({ @AssociationOverride(name = "pk.clinic", joinColumns = @JoinColumn(name = "clinic")),
        @AssociationOverride(name = "pk.doctor", joinColumns = @JoinColumn(name = "doctor")) })
public class ClinicDoctor
{
    @EmbeddedId
    private ClinicDoctorId pk = new ClinicDoctorId();

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

    public ClinicDoctorId getPk()
    {
        return pk;
    }

    public void setPk(ClinicDoctorId pk)
    {
        this.pk = pk;
    }

    public String getAttendingHours()
    {
        return attendingHours;
    }

    public void setAttendingHours(String attendingHours)
    {
        this.attendingHours = attendingHours;
    }

    // Equals & HashCode
}

// ClinicDoctorId

public class ClinicDoctorId implements Serializable
{
    private static final long serialVersionUID = 5880105185191860784L;

    @ManyToOne
    private Clinic clinic;

    @ManyToOne
    private Doctor doctor;

    public Clinic getClinic()
    {
        return clinic;
    }

    public void setClinic(Clinic clinic)
    {
        this.clinic = clinic;
    }

    public Doctor getDoctor()
    {
        return doctor;
    }

    public void setDoctor(Doctor doctor)
    {
        this.doctor = doctor;
    }

    // Equals & HashCode
}

我想列出诊所名称为“X”的所有关系。到目前为止,我最好的尝试是:

Criteria criteria = session.createCriteria(ClinicDoctor.class);
criteria.createAlias("pk.clinic", "clinic").add(Restrictions.eq("clinic.name", "X"));
res = new ArrayList<>(criteria.list());

然而,这会导致以下Hibernate查询和错误:

Hibernate: 
    select
        this_.clinic as clinic2_1_0_,
        this_.doctor as doctor3_1_0_,
        this_.attendingHours as attendin1_1_0_ 
    from
        clinic_doctor this_ 
    where
        clinic1_.name=?

ERROR: Unknown column 'clinic1_.name' in 'where clause'

老实说,我不知道为什么这是错的。我相信之前已经这样做了,但是在某些地方,我无法看到(或理解)阻止此查询工作的东西。有人可以帮我一把吗?

编辑:如下所示,我尝试了这个:

Query query = session.createQuery("SELECT cd FROM ClinicDoctor cd WHERE cd.doctor.name = :docname");
query.setParameter("docname", X);

它完美无缺。但是,我更喜欢基于Criteria的答案,或者至少解释为什么我的初始查询不起作用。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我看到的第一件事是

where
        clinic1_.name=?

当你在问题中说:

  

我想列出医生姓名为'X'的所有关系。

因此,您的查询正试图在诊所的名称为“X”

的情况下进行关系

如果你希望第一个尝试这个:

Query query = session.createQuery("SELECT cd FROM ClinicDoctor cd WHERE cd.doctor.name = :docname");
query.setParameter("docname", X);