QueryDSL to get any entities in a collection of another entity

时间:2015-10-30 22:05:29

标签: java hibernate jpa querydsl

I'm using JPA with Hibernate and QueryDSL (v.4.0.5). I have this entity:

package com.test.model.entity;

@Entity
public class Article {
    @Id
    private Long id;

    @ManyToMany(fetch = LAZY, cascade = DETACH)
    private Set<Tag> tags;
}

How can I find all the articles matching a given set of Tags? I think I should start as follows:

public BooleanExpression hasTag(Set<Tag> tags){
    final QArticle article = QArticle.article;
    return article.tags.any().eqAny(ce);
}

where ce should be a CollectionExpression. Clearly I have no idea how to set this.

Any solution?

1 个答案:

答案 0 :(得分:4)

你试过吗

2.2.3 :001 > gs2 = GatheringSession.last
 => #<GatheringSession id: 120, gathering_state: 2 ... > 
2.2.3 :002 > gs2.gathering_state
 => 2 
2.2.3 :003 > gs2.ready?
 => false 
2.2.3 :004 > gs2.finished?
 => false 
2.2.3 :005 > gs2.ready!
   (0.2ms)  BEGIN
     SQL (0.8ms)  UPDATE `gathering_sessions` SET `gathering_state` = 'ready', `updated_at` = '2015-10-31 00:28:36' WHERE `gathering_sessions`.`id` = 120
     Mysql2::Error: Incorrect integer value: 'ready' for column 'gathering_state' at row 1: UPDATE `gathering_sessions` SET `gathering_state` = 'ready', `updated_at` = '2015-10-31 00:28:36' WHERE `gathering_sessions`.`id` = 120
   (0.1ms)  ROLLBACK
     ActiveRecord::StatementInvalid: Mysql2::Error: Incorrect integer value: 'ready' for column 'gathering_state' at row 1: UPDATE `gathering_sessions` SET `gathering_state` = 'ready', `updated_at` = '2015-10-31 00:28:36' WHERE `gathering_sessions`.`id` = 120
     ...
     from /Users/william/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/persistence.rb:263:in `update!'
     from /Users/william/code/repo/config/initializers/enum_patch.rb:193:in `block (4 levels) in enum'