Have 2 entities
@Table(name = "REQUEST")
public class RequestEntity {
@Id
@Column(name = "REQUEST_ID")
private String requestId;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "REQUEST_ID")
private Set<StatusLogEntity> statusLogs;
... setters/getters
}
and
@Table(name = "STATUS_LOG")
public class StatusLogEntity {
@Id
@GeneratedValue
private Long statusLogId;
@Column(name = "REQUEST_ID")
private String requestId;
...setters/getters
}
Need to provide the JOIN that populates the StatusEntity Objects set.
The following Specification only returns RequestEntity
, when asking for the requestEntity.getStatusLogs()
the expected error
could not initialize proxy - no Session
due to the (FetchType.LAZY) and that the StatusLogs are not being hydrated from the query.
The Specification I have is:
public static Specification<RequestEntity> withLogs(
final String requestId) {
return (root, query, builder) -> {
Root<StatusLogEntity> statusLogRoot = query.from(StatusLogEntity.class);
root.join("statusLogs").on(builder.equal(statusLogRoot.get("requestId"), requestId));
return builder.equal(root.get("requestId"), accountActionRequestId);
};
}
Which generates the following query
request0_.request_id as request_1_0_,
from
request request0_ cross
join
status_log statusloge1_
inner join
status_log statuslogs2_
on request0_.request_id=statuslogs2_.request_id
and (
statusloge1_.request_id=?
)
where
request0_.request_id=?
How can I create a Specification that returns the Request with their respective StatusLogs?
Thanks!