I have parent/child relationship between two tables, and the corresponding mapping in my Java classes. The tables roughly look like that:
A (ref number, stuf
It is not supported by JPA but if you are using hibernate as JPA provider then you can use annotation @FilterDef and @Filter.
Hibernate Core Reference Documentation
Hibernate3 has the ability to pre-define filter criteria and attach those filters at both a class level and a collection level. A filter criteria allows you to define a restriction clause similar to the existing "where" attribute available on the class and various collection elements. These filter conditions, however, can be parameterized. The application can then decide at runtime whether certain filters should be enabled and what their parameter values should be. Filters can be used like database views, but they are parameterized inside the application.
Exemple
@Entity
public class A implements Serializable{
@Id
@Column(name = "REF")
private int ref;
@OneToMany
@JoinColumn(name = "A_REF", referencedColumnName = "REF")
@Filter(name="test")
private Set bs;
}
@Entity
@FilterDef(name="test", defaultCondition="other = 123")
public class B implements Serializable{
@Id
@Column(name = "A_REF")
private int aRef;
@Id
@Column(name = "OTHER")
private int other;
}
Session session = entityManager.unwrap(Session.class);
session.enableFilter("test");
A a = entityManager.find(A.class, new Integer(0))
a.getb().size() //Only contains b that are equals to 123