检查实体是否在映射实体列表中

时间:2015-04-16 16:52:33

标签: java spring hibernate

我有以下Hibernate代码,我认为应该正常工作,但它会引发错误:

  

org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是org.hibernate.exception.SQLGrammarException:错误:语法错误在“。”或附近。     位置:503

相关代码如下:

@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();
}

RepositoryLink如下:

@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;
    }
}

我可能会使用一些连接,但我认为如果我能使in语法工作会更好,为什么它不能像这样工作?

1 个答案:

答案 0 :(得分:5)

我不能直接详细说明为什么这样做,而不是自己检查hibernate文档。 ;)

在您的查询中使用此项:IN elements(link.dugaUsers)