简体   繁体   中英

Hibernate query for multiple associations

I have the following structure in JAVA.

public class Article {
    private long id;
    private Source source;
}

public class Source {
    private long id;
    private Type type;
}

public class Type {
    private long id;
    private String sourceType;
}

How do I query all articles with Type.id = somevalue using Hibernate Criteria. Right now I can only query until Source class like this

Criteria query = currentSession().createCriteria(Article.class)
            .createAlias("source", "s")
            .add(Restrictions.eq("s.id", Long.parseLong(typeId)));

Try this

Criteria query = currentSession().createCriteria(Article.class)
        .createAlias("source", "s")
        .createAlias("s.type","t")
        .add(Restrictions.eq("t.id", Long.parseLong(typeId)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM