That would work for a join table but this isnt a typical join table.
In my use case I want to join on ItemImages where ItemImage.item_id = Item.id and then join on ItemImageColors where ItemImageColor.item_image_id = ItemImage.id
I can somewhat get the behavior I'm looking for by setting eager load true on all associations...
Code:
class Item {
@OneToMany(mappedBy = "item", fetch = FetchType.EAGER)
private Set<ItemImage> itemImages;
}
class ItemImage {
@OneToMany(mappedBy = "itemImage", fetch = FetchType.EAGER)
private Set<ItemImageColor> itemImageColors;
}
class ItemImageColor {
@ManyToOne
@JoinColumn(name = "item_image_id")
private ItemImage itemImage;
}
I then need to loop through all images and their colors to get the final result which is less than ideal. I would like to simply do the following
Code:
for (ItemImageColor color : item.getItemImageColors) {
....
}
davided80 wrote:
Abut the mapping, this part of the documentation should explain how to do that: http://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html/ch07.html#example.collection.mapping.annotations
Thanks but this is for a join table where the join table has a reference to the other tables.. ie ItemImage has a reference to Item and ItemImageColor.
In my use case the table ItemImage has a reference to Item and ItemImageColor has a reference to ItemImage.
Looking at the documentation, it should be something like this:
Code:
sess.createCriteria(Item.class)
.add( Restrictions.like("id", id) )
.createAlias("itemImageColors", "color")
.setProjection( Projections.property("color.colorValue"), "color" )
.list();
These are the part of the documentation I've used:
- http://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html/ch17.html#querycriteria-associations
- http://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html/ch17.html#querycriteria-projection