Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if Entity is in list of mapped entities

I have the following Hibernate code which I believe should be working, but it throws an error:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.exception.SQLGrammarException: ERROR: syntax error at or near "." Position: 503

The relevant code is the following:

@Override
public List<RepositoryLink> getRepositoryLinks(final DugaUser user) {
    Query query = sessionFactory.getCurrentSession()
        .createQuery("from RepositoryLink link where :user in (link.dugaUsers)");
    query.setParameter("user", user);
    return query.list();
}

Where RepositoryLink is the following:

@Entity
@Table(name = "repository_links")
public class RepositoryLink {
    @Id
    @GeneratedValue
    private Integer id;

    @OneToOne
    @JoinTable(name = "repository_links_github_repositories",
        joinColumns = {@JoinColumn(name = "github_repository_id", referencedColumnName = "id")},
        inverseJoinColumns = {@JoinColumn(name = "repository_link_id", referencedColumnName = "id")})
    private GithubRepository githubRepository;

    @OneToMany
    @JoinTable(name = "repository_links_duga_users",
        joinColumns = {@JoinColumn(name = "duga_user_id", referencedColumnName = "id")},
        inverseJoinColumns = {@JoinColumn(name = "repository_link_id", referencedColumnName = "id")})
    private List<DugaUser> dugaUsers = new ArrayList<>();

    public Integer getId() {
        return id;
    }

    public GithubRepository getGithubRepository() {
        return githubRepository;
    }

    public void setGithubRepository(final GithubRepository githubRepository) {
        this.githubRepository = githubRepository;
    }

    public List<DugaUser> getDugaUsers() {
        return dugaUsers;
    }

    public void setDugaUsers(final List<DugaUser> dugaUsers) {
        this.dugaUsers = dugaUsers;
    }
}

I could probably get it working with some joins, but I thought it would be nicer if I could get the in syntax to work, why doesn't it work like this?

like image 736
skiwi Avatar asked Apr 16 '15 16:04

skiwi


1 Answers

I cannot elaborate directly anymore why this works, without checking the hibernate docs myself. ;)

Use this in your query: IN elements(link.dugaUsers)

like image 84
Martin Frey Avatar answered Sep 28 '22 10:09

Martin Frey