简体   繁体   中英

Hibernate Criteria with restriction on children

I got a simple class

@Entity(name = "tag")
public class Tag
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    protected long id;

    @Column(name = "tag", nullable = false)
    private String tag;

    public Tag()
    {
    }

    public long getId()
    {
        return id;
    }

    public void setId(long id)
    {
        this.id = id;
    }

    public String getTag()
    {
        return tag;
    }

    public void setTag(String tag)
    {
        this.tag = tag;
    }
}

And it resides as list in another class and is obtained using

@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name = "entry_tag_list", joinColumns = {
        @JoinColumn(referencedColumnName = "id", nullable = false)}, inverseJoinColumns = {
        @JoinColumn(referencedColumnName = "id", nullable = false)})
@Fetch(FetchMode.SELECT)
private List<Tag> tags;

So when I create a Criteria using

cr.add(Restrictions.eq("tags.tag", tag));

I get

Caused by: org.hibernate.QueryException: could not resolve property: tags.tag

I made sure that my getters and setters are getTags and setTags, so I am not sure what I am missing.

As a sidenote, I have to use @Fetch(FetchMode.SELECT) since a lot of queries depend on it.

Try like this

cr.createAlias("tags", "tagsAlias");
cr.add(Restrictions.eq("tagsAlias.tag", tag);

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