java – Hibernate:标准与集合

前端之家收集整理的这篇文章主要介绍了java – Hibernate:标准与集合前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个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
}
原文链接:https://www.f2er.com/java/121648.html

猜你在找的Java相关文章