JPA查询语言中的IN和=运算符

时间:2013-04-25 07:12:42

标签: java eclipselink jpa-2.0 jpql

我有关于jpa查询的问题。 有两个表,即Post表和Tag表 Post和Tag之间有很多关系

现在我想编写一个查询,以便在选择多个标签时,应选择与这些标签关联的所有帖子。 例如,

post1 has tags friends and motivation
post2 has tags motivation and pune
post3 has tag boxing

如果选择标签friends和pune,则应检索post1和post2 如果选择标签装箱,那么只应检索第3个标签 如果选择标签拳击和动机,则应检索所有三个帖子。

我尝试了以下内容

SELECT DISTINCT p FROM Post p JOIN p.tags tags WHERE p.tags IN :tags

但是它给出了验证者错误

The state field path 'p.tags' cannot be resolved to a collection type.

如果我这样尝试

SELECT DISTINCT p FROM Post p JOIN p.tags tags WHERE p.tags = :tags

然后它符合要求但在传递标签列表后它会给出错误

 java.lang.IllegalArgumentException: You have attempted to set a value of type class java.util.ArrayList for parameter tags with expected type of class com.justme.model.entities.Tag from query string SELECT DISTINCT p FROM Post p JOIN p.tags tags WHERE p.tags = :tags.

感谢您阅读这篇文章:)请您指导我吗?

如何实现上述结果? 我的持久性提供程序是eclipseLink

这是邮政实体

@Entity
@NamedQueries({
    @NamedQuery(name = "Post.selectAllPosts", query = "SELECT p FROM Post p ORDER BY p.dateCreated DESC"),
    @NamedQuery(name = "Post.selectPostForUser", query = "SELECT p FROM Post p WHERE p.user = :user ORDER BY p.dateCreated DESC"),
    @NamedQuery(name = "Post.selectPostsByTags", query = "SELECT DISTINCT p FROM Post p JOIN p.tags tags WHERE p.tags IN :tags") })
public class Post implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int idpost;

@Lob
private String content;

private String title;

// bi-directional many-to-one association to User
@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "iduser")
private User user;

// bi-directional many-to-many association to Tag
@ManyToMany(cascade = CascadeType.PERSIST)
@JoinTable(name = "post_tag", joinColumns = @JoinColumn(name = "idpost"),         inverseJoinColumns = @JoinColumn(name = "idtag"))
private List<Tag> tags = new ArrayList<Tag>();

@Temporal(TemporalType.DATE)
private Date date = null;
@Temporal(TemporalType.TIMESTAMP)
private Date dateCreated = new Date();

public Post() {
}

public int getIdpost() {
    return this.idpost;
}

public void setIdpost(int idpost) {
    this.idpost = idpost;
}

public String getContent() {
    return this.content;
}

public void setContent(String content) {
    this.content = content;
}

public String getTitle() {
    return this.title;
}

public void setTitle(String title) {
    this.title = title;
}

public User getUser() {
    return this.user;
}

public void setUser(User user) {
    this.user = user;
}

public List<Tag> getTags() {
    return this.tags;
}

public void setTags(List<Tag> tags) {
    this.tags = tags;
}

public Date getDate() {
    return date;
}

public void setDate(Date date) {
    this.date = date;
}

public Date getDateCreated() {
    return dateCreated;
}

public void setDateCreated(Date dateCreated) {
    this.dateCreated = dateCreated;
}

@Override
public String toString() {
    return "Post [idpost=" + idpost + ", content=" + content + ", title="
            + title + ", date=" + date + "]";
}

}

这是标记实体

@Entity
@NamedQueries({
    @NamedQuery(name = "Tag.selectTags", query = "SELECT tag FROM Tag tag WHERE tag.tagName LIKE :keyword"),
    @NamedQuery(name = "Tag.selectMatchingTags", query = "SELECT t.tagName FROM Tag t WHERE t.tagName LIKE :keyword"),
    @NamedQuery(name = "Tag.selectTagByName", query = "SELECT tag FROM Tag tag WHERE tag.tagName = :tagName"),
    @NamedQuery(name = "Tag.selectTagsForAllPosts", query = "SELECT DISTINCT tag FROM Tag tag, Post post JOIN tag.posts posts WHERE post.user = :user")})

public class Tag implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int idtag;

private String tagName;

// bi-directional many-to-many association to Post
@ManyToMany(mappedBy = "tags", cascade = CascadeType.PERSIST)
private List<Post> posts;


public Tag() {

}

public Tag(String tagName) {
    this.tagName = tagName;
}


public int getIdtag() {
    return this.idtag;
}

public void setIdtag(int idtag) {
    this.idtag = idtag;
}

public String getTagName() {
    return this.tagName;
}

public void setTagName(String tagName) {
    this.tagName = tagName;
}

public List<Post> getPosts() {
    return this.posts;
}

public void setPosts(List<Post> posts) {
    this.posts = posts;
}

@Override
public String toString() {
    return tagName;
}

}

1 个答案:

答案 0 :(得分:7)

尝试:

    ...
    @NamedQuery(name = "Post.selectPostsByTags", query =
       "SELECT DISTINCT p FROM Post p JOIN p.tags tags WHERE tags IN (:tags)") })
public class Post implements Serializable {
    ...

像这样使用:

@PersistenceContext
public EntityManager em;
...
List<Tag> ltags = new ArrayList<Tag>();
ltags.add(tagOne);
ltags.add(tagTwo);
List<?> list = em.createNamedQuery("Post.selectPostsByTags")
                                                    .setParameter("tags", ltags)
                                                    .getResultList();

for (Object object : list) {
    System.out.println("Results: "+object);
}