我有一个hibernate和criteria的问题.我有两个班:
public class Place{ long id; String name; Set<Street> streets; } public class Street{ long id; String name; Place place; }
我现在想编写一个方法,它返回一个名称列表的方法,名称如参数中所给出,并且一个名为“参数”的街道.
public List<Place> findPlaces(String name,String streetname){ //getSession() gives me a hibernate session Criteria crit = getSession().createCriteria(Place.class,"place"); crit.add(Restrictions.like("name",name+"%")); //Everything works fine until here //Last step: Sort out all places not containing a street named like streetname + "%" }
我尝试了最后一步的不同方法:
//streetList is a list of all streets named like streetname crit.add(Restrictions.in("streets",streetList));
其他方式:
DetachedCriteria strasseCrit = DetachedCriteria.forClass(Street.class,"street"); streetCrit.add(Restrictions.like("street.name",streetname + "%")); streetCrit.createAlias("street.place","streetPlace"); streetCrit.add(Restrictions.eqProperty("streetPlace.id","place.id")); streetCrit.setProjection(Projections.property("street.name")); crit.add(Subqueries.exists(streetCrit));
最后方式:
crit.createAlias("place.streets","street"); crit.add(Restrictions.like("street.name",streetname + "%")); crit.setResultTransformer(DistinctResultTransformer.INSTANCE);
我希望你能理解我的问题,抱歉我的坏英文:(
德国问候:)
菲利普
解决方法
public List<Place> findPlaces(String name,String streetname){ Criteria crit = getSession().createCriteria(Place.class,"place"); criteria.createAlias("streets","s"); // Create alias for streets crit.add(Restrictions.like("s.name",name+"%")); // continue method }