I have a class with a many-to-many association(implemented using many-to-one associations and an intermediary class.) i.e.
Product <-> ProductMetaData <-> MetaData
I'm trying to write a Criteria that selects Products that have a pre-defined set of MetaData, and I'm not having any luck.
I have the following sql. (Probably not very efficient/elegant due to my inexperience with SQL, but it does seem to work.)
Code:
select p.product_id from product p, product_validation_data pvd0, product_validation_data pvd1 where p.product_id=pvd0.product_fk and p.product_id=pvd1.product_fk and pvd0.metadata_fk=4 and pvd1.metadata_fk=5;
I've tried a number of scenarios, mostly resorting to conjunction's e.g.
Code:
DetachedCriteria productCriteria = DetachedCriteria
.forClass(Product.class)
.createAlias("productMetaData", "pmd");
Conjunction conjunction = Restrictions.conjunction();
conjunction.add(Restrictions.eq("pmd.metaData", metaData1));
conjunction.add(Restrictions.eq("pmd.metaData", metaData2));
productCriteria.add(conjunction);
But I dont seem to be able to create the appropriate alias'. Heres the Hibernate generated code (probably doesnt match the example above as my code keeps changing, but they all seem to have the same problem of only having a single alias.
Code:
select this_.product_id as product1_3_ from product this_ inner join product_validation_data productval1_ on this_.product_id=productval1_.product_fk inner join metadata metadata2_ on productval1_.metadata_fk=metadata2_.metadata_id where ( (metadata2_.metatag_fk=4 and metadata2_.value='1234567') and (metadata2_.metatag_fk=5 and metadata2_.value='12345'));
I'm sure I'm missing something simple. Anyone got any ideas?
Hibernate Version:3.0.5