This should be pretty east to do with a criteria query.
Just create an example class and set the property you're looking for to "publisher". Then, send that Example, which implements the Hibernate Criterion interface, to a findByCriterion method in a DAO.
It might look something like this:
Code:
package com.examscam.criteria;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import com.examscam.HibernateUtil;
import com.examscam.model.User;
public class FindVerifiedUsers {
public static void main(String[] args) {
User user = new User();
user.setVerified(false);
Example example = Example.create(user);
Session session = HibernateUtil.beginTransaction();
Criteria criteria = session.createCriteria(User.class);
criteria.add(example);
List results = criteria.list();
HibernateUtil.commitTransaction();
for (int i = 0; i<results.size(); i++) {
System.out.println(results.get(i).toString());
}
}
}
From the Hibernate3 Tutorial on using the Hibernate Criteria API and JPA AnnotationsThis will work well with a findByExample or findByCriterion method in a Data Access Object:
Code:
package com.examscam.criteria;
import java.util.List;import org.hibernate.*;
import org.hibernate.criterion.*; import com.examscam.model.User;
import com.examscam.HibernateUtil;
public class UserHelper {
public static List findByCriterion(Criterion... criterion){
Session session = HibernateUtil.getSession();
Criteria criteria=session.createCriteria(User.class);
for (Criterion crit : criterion) {
criteria.add(crit);
}
return criteria.list();
}
}
Code:
public List<T> findByExample(T exampleInstance,
String[] excludeProperty) {
Criteria crit = HibernateUtil.getSession()
.createCriteria(persistentClass);
Example example = Example.create(exampleInstance);
for (int i=0; i< excludeProperty.length; i++) {
example.excludeProperty(excludeProperty[i]);
}
crit.add(example);
return crit.list();
}
Creating Advanced Data Access Objects with Hibernate: Tutorial and Examples