tl;dr
Can’t make Hibernate filter work with an embedded id property.
Sample project to reproduce the issue here
Some Introduction
Hey there. I’m new to this forum so if I did something wrong I’ll fix it as soon as possible if you guys point out.
I’m creating this question after I tried to fix the problem with help from this post on stackoverflow (many thanks to @Andronicus there).
There is a sample project to reproduce the issue. Link in the first section (can’t put more links in this post)
The following section contains my question copied from stackoverflow.
Actual question
I’m struggling with this query for quite a while.
Suppose the following entity mapping example:
@Entity
class Client {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "client_id")
@Basic(optional = false)
private Integer id;
@Basic(optional = false)
private String name;
@OneToMany(mappedBy = "client")
private List<CarRent> rentHistory;
// ... getters and setters
}
@Entity
class Car {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "car_id")
@Basic(optional = false)
private Integer id;
@Basic(optional = false)
private String foo;
// ... getters and setters
}
@Entity
class CarRent {
@EmbeddedId
private CarRentKey carRentKey;
@MapsId("clientId")
@ManyToOne()
@JoinColumn(name = "client_id", nullable = false, insertable = false, updatable = false)
private Client client;
@MapsId("carId")
@ManyToOne()
@JoinColumn(name = "car_id", nullable = false, insertable = false, updatable = false)
private Car car;
@Basic(optional = false)
private String bar;
// ... getters and setters
}
@Embeddable
class CarRentKey {
private int clientId;
private int carId;
@Column(name = "date_due")
private Date dateDue;
// ... getters and setters
}
I need to fetch all Clients with theis rentHistory populated with CarRents from a certain date. The following query would work perfectly for me:
from Client cl
left outer join fetch c.rentHistory as rent with rent.car = c and rent.dateDue = :date
But Hibernate keeps telling me to use a filter when fetching a join in the exception.
I tryed
@Entity
@FilterDef(name="dateDueFilter", parameters= {
@ParamDef( name="dateDue", type="date" ),
})
@Filters( {
@Filter(name="dateDueFilter", condition="dateDue = :dateDue"),
})
class CarRent {
// ...
}
but then when I run my query like:
EntityManager em;
// ...
Session hibernateSession = em.unwrap(Session.class);
hibernateSession.enableFilter("dateDueFilter").setParameter("dateDue", dateDue);
em.createQuery("from Client cl"
+ "left outer join fetch c.rentHistory");
List<Client> clientList = q.getResultList();
// clientList contains CarRent of all dates
The filter is just ignored. Same result with condition="carRentKey.dateDue = :dateDue"
and condition="date_due = :dateDue"
.
I use filters for other left outer joins on the same query and they work just fine. But this one relation that evolves an embedded parameter I can’t find a way to make it work.
Is it possible? Are there alternatives?
PS: filtering in the where section e.g. from Client cl left outer join fetch c.rentHistory as rent where rent.dateDue is null or rent.dateDue = :date
is not an option since my real query has other joins which results get filtered and gets really slow when I do so.