I had a hard time with it since yesterday and I found out a way to make it generate the join between the tables. I am not sure if this is a bug, but I couldn't find anyone telling if this behaviour is correct (nor any workaround). I think it is worth to file a bug report and see what the core developers say.
However, I made it work. In my case, the code was pretty much like yours, except that I use annotations (but they don't make difference for this problem). I had a composite key defined in another class, and this key was composed of an integer and a foreign key:
Code:
@Embeddable(access = AccessType.FIELD)
public static class MarkerAlelePk implements Serializable {
@ManyToOne
@JoinColumn(name="marker_id")
private MarkerBean marker;
@Column(name="alele_id")
private int alele;
I wanted to have a list where the marker.name was a given parameter, but Criteria didn't generate the join, as you said. So I declared, in my main class a property named marker, with the same configuration as the key in the above composite key, with one difference: not updateable and not insertable.
Code:
@ManyToOne
@JoinColumn(name="marker_id",referencedColumnName="marker_id",insertable=false,updatable=false)
public MarkerBean getMarker() {
return getPk().getMarker();
}
public void setMarker(MarkerBean marker) {
getPk().setMarker(marker);
}
Note that the above annotations are set for field and the below for property, because I didn't have this field.
Now my criteria uses this marker property (from the main class) and the join is generated correctly and my query is performed. Any comments about this approach are very welcome.