In my database, I have publications and each publication can have zero or more pages, which are stored in another table. When selecting them, I either want to get all publications or only the ones with at least one page. The following criteria query is not working because of this error: org.hibernate.QueryException: could not resolve property: p
Criteria c = Utils.getSession().createCriteria(Publication.class);
if(!showEmptyPublications)
{
ProjectionList pl = Projections.projectionList();
pl.add(Projections.count("pages").as("p"));
c.setProjection(pl);
c.add(Restrictions.gt("p", 0));
}
c.addOrder(Order.asc("titel"));
publications = c.list();
Publication:
CREATE TABLE IF NOT EXISTS `Publication` (
`id` int(11) NOT NULL AUTO_INCREMENT,
...
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=47 ;
Page:
CREATE TABLE IF NOT EXISTS `Page` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`pub_id` int(11) NOT NULL,
...
PRIMARY KEY (`id`),
KEY `FOREIGN_KEY_DICT` (`pub_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6314 ;
ALTER TABLE `Page`
ADD CONSTRAINT `FOREIGN_KEY_PUBLICATION` FOREIGN KEY (`pub_id`)
REFERENCES `Publication` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
Publication.java:
...
@OneToMany(fetch = FetchType.LAZY, mappedBy = "publication")
public Set<Page> getPages()
{
return this.pages;
}
public void setPages(Set<Page> pages)
{
this.pages = pages;
}