Java – Hibernate: standards and collections

I have a question about hibernate and criteria I have two classes:

public class Place{
    long id;
    String name;
    Set<Street> streets;
}

public class Street{
    long id;
    String name;
    Place place;
}

Now I want to write a method that returns a method with a list of names, as given in the parameter, and a street named "parameter"

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 + "%"
}

I tried a different approach to the last step:

//streetList is a list of all streets named like streetname
crit.add(Restrictions.in("streets",streetList));

Other methods:

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));

Final method:

crit.createAlias("place.streets","street");
crit.add(Restrictions.like("street.name",streetname + "%"));
crit.setResultTransformer(DistinctResultTransformer.INSTANCE);

I hope you can understand my problem. I'm sorry for my bad English:(

I searched the solution for two days and I don't know how to get there

Greetings from Germany:) Philip

Solution

public List<Place> findPlaces(String name,String streetname){
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
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>