Play!Framework:在集合上使用find()模型方法

时间:2011-09-05 11:27:16

标签: java collections model playframework find

让我们假设一个带有theese字段的用户类:

@Entity
public class User extends Model {
    public String email;
    public String password;
    @ElementCollection
    public List<String> stringList;
}

我正在寻找一种方法来执行数据库请求,以查找在stringList中具有给定字符串的所有用户,例如

List<User> usersHelloWorld = User.find("byStringList", "HelloWorld").fetch();

但当然,这不起作用。有没有办法让它发挥作用?

修改:

以下是我的实际课程字段:

@Entity
public class User extends Model {
    public String email;
    public String password;
    public String firstname;
    public String lastname;
    public String gender;
    public String fbId;
    public String googleId;
    @ElementCollection
    public List<String> eventTopicIds;
    @Transient
    UserEventBuffer eventBuffer;
    @Transient
    public String fbAccessToken;
    public String googleAccessToken;
}

有了这个请求:

List<User> u = User.find("SELECT u from User u where ? IN u.eventTopicIds", "internalns_rootTopic1").fetch();

我收到了这个错误:

  

发生JPAQueryException:执行查询SELECT时出错   来自用户你在哪里? IN u.eventTopicIds:SQL语句中的语法错误   “SELECT USER0_.ID AS ID3_,USER0_.EMAIL AS EMAIL3_,USER0_.FBID AS   FBID3_,USER0_.FIRSTNAME AS FIRSTNAME3_,USER0_.GENDER为GENDER3_,   USER0_.GOOGLEACCESSTOKEN作为GOOGLEAC6_3_,USER0_.GOOGLEID AS   GOOGLEID3_,USER0_.LASTNAME AS LASTNAME3_,USER0_.PASSWORD AS   PASSWORD3_ FROM USER USER0_ CROSS JOIN USER_EVENTTOPICIDS EVENTTOPIC1_   WHERE USER0_.ID = EVENTTOPIC1_.USER_ID AND(?IN(。[*]))“;预期   “NOT,EXISTS,SELECT,FROM”; SQL语句:选择user0_.id为id3_,   user0_.email as email3_,user0_.fbId as fbId3_,user0_.firstname as   firstname3_,user0_.gender为gender3_,user0_.googleAccessToken为   googleAc6_3_,user0_.googleId为googleId3_,user0_.lastname为   lastname3_,user0_.password as password3_ from User user0_ cross join   User_eventTopicIds eventtopic1_其中user0_.id = eventtopic1_.User_id   和(?in(。))[42001-149]

1 个答案:

答案 0 :(得分:2)

通常,您应该能够运行像

这样的JPA查询
User.find("Select u from User as u inner join u.stringList as strings where ? in strings", "HelloWorld").fetch();

做了一个小测试,我的对象有电子邮件而不是字符串,但它应该是相同的,除非你遇到一些保留的话。问题不在于关键字,我必须内部加入stringList才能使用in关键字。稍早一点标记:)

希望这有帮助。