In my Hibernated system, I have a class Picture that has some simple data, a list of Installations and some (huge) binary data. The list of Installations is implemented with a join table, as it's a ManyToMany relation, and it's defined on the Installation side:
@Entity
public class Picture {
@Id
private long pictureId;
private String mimeType;
@Lob
@Basic(fetch=FetchType.LAZY) // Don't load all data unless required
private byte[] picture;
@ManyToMany(mappedBy= "images")
private List<Installation> installations;
/** This should be used only to load partial objects through PictureDao */
public Picture(long pictureId, String mimeType, List<Installation> insts) {
this.pictureId = pictureId;
this.mimeType = mimeType;
this.installations = insts;
}
...
}
The constructor is there solely to allow partial selects.
@Entity
public class Installation {
@Id
private long id;
@ManyToMany(cascade=CascadeType.PERSIST)
@JoinTable(name="InstPicture",
joinColumns={@JoinColumn(name="installationId")},
inverseJoinColumns = {@JoinColumn(name="pictureId")})
@OrderBy("pictureDate ASC")
private List<Picture> images;
...
}
I'd like to be able to make a partial select that inlucdes the list of Installations, but not the picture data. I have tried the obvious solution:
Query q = em.createQuery("SELECT new Picture" +
"(p.pictureId, p.mimeType, p.installations) " +
" FROM Picture p WHERE p.pictureId = :id");
but it claims it cannot find the constructor. If I don't include the installations in the select, the field becomes null rather than a lazy loader for the list. I have also tried joining p.installations in, and it still can't find the constructor. Without the list of Installations, it works beautifully.
Is there a way to get a Collection into my partial select?
Thanks, -Lars