如何实现一对一关系中的条件

时间:2018-09-06 18:44:18

标签: hibernate jpa

我正在尝试在JPA中实现以下“示例”查询。这是一对一的关系。

从“人”的人中选择*,年龄为age.minAge和age.maxAge之间的人

在我的真实项目中,我有多个类似这样的Where条件,我想知道使用批注实现相同效果的最有效方法。

谢谢。

@Entity
@Table(name = "PERSON")
public class Person 
{
    @Column(name = "NAME", nullable = false, precision = 0)
    private String name;

    @Column(name = "ADDRESS", nullable = false, precision = 0)
    private String address;

    @Column(name = "AGE", nullable = false, precision = 0)
    private int age;

    private BMIForAge bmi_age;

    **@OneToOne(optional = false, fetch = FetchType.EAGER)
    public BMIForAge getBmi_age() {
        return bmi_age;
        }

        public void setBmi_age(BMIForAge bmi_age) {
        this.bmi_age = bmi_age;
    }**
}


@Entity
@Table(name = "BMI_FOR_AGE")
public class BMIForAge
{
    @Column(name = "MIN_AGE", nullable = false, precision = 0)
    private int minAge;

    @Column(name = "MAX_AGE", nullable = false, precision = 0)
    private int maxAge;

    @Column(name = "BMI", nullable = false, precision = 0)
    private int bmi;
...
}

1 个答案:

答案 0 :(得分:0)

使用JPQL,您将获得类似:

select p from Person p join p.bmi_age b where p.age between b.minAge and b.maxAge;