我对 hibernate 和标准有疑问。我有两个类(class):
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
}
关于java - Hibernate:集合条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9982823/