在JPA中选择一对多关系的孩子

时间:2014-12-18 07:46:06

标签: java hibernate jpa jpql

我有这个实体:

@Entity
public class Person {

    @Id 
    @GeneratedValue 
    private long id;

    private String firstName;
    private String lastName;

    @Column(unique = true )
    private String token;

    @OneToMany(cascade = CascadeType.ALL)
    private List<Task> tasks;

与此实体的OneToMany关系:

@Entity
public class Task {
    @Id 
    @GeneratedValue 
    private long id;

    private double time;
    private String date;

现在,如果我想从日期为“2014-12-12”的人那里获取所有任务,JPQL查询怎么样?

1 个答案:

答案 0 :(得分:0)

@Entity
public class Person {

    @Id 
    @GeneratedValue 
    private long id;

    private String firstName;
    private String lastName;

    @Column(unique = true )
    private String token;

    @OneToMany(mappedBy="person", cascade = CascadeType.ALL)
    private List<Task> tasks;

@Entity
public class Task {

    @Id 
    @GeneratedValue 
    private long id;

    @ManyToOne
    @JoinColumn(name = "person_id")
    private Person person;

    private double time;
    private String date;
}

select t from Task t where t.person.id = ? and t.date = ?