JPA (hibernate) relation OneToMany with date
I need to create hibernate relation one to many between Department and Person (one Department has many persons).
The problem is that I want to maintain time validity. Without ORM (hibernate) it is easy to select persons of particular department at specified date:
select * from Person whe开发者_JAVA技巧re department_id = ? and ? between validFrom and validTill
How can I do it with JPA hibernate?
class Department {
Long id;
String code;
String name;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "departmentId")
@OnDelete(action = OnDeleteAction.CASCADE)
private Set<Person> persons = new HashSet<Person>();
}
class Person {
Long id;
String name;
String surname;
Date validFrom;
Date validTill;
}
And how to do it if Department is time valid too?
select P.* from Person P, Deparment d
where d.code = ? and
p.department_id = d.department_id and
? between d.validFrom and d.validTill and
? between p.validFrom and p.validTill
All records with same deparment.code is one department.
You do it in JPA the same way that you're doing it with SQL. Just make it a JPQL query rather that a SQL query:
select p from Person p
inner join p.department d
where d.code = :departmentCode
and :today between d.validFrom and d.validTill
and :today between p.validFrom and p.validTill
精彩评论