开发者

Play!Framework : using find() model method on a collection

开发者 https://www.devze.com 2023-04-02 06:32 出处:网络
Let\'s assume a User class with theese fields : @Entity public class User extends Model { public String email;

Let's assume a User class with theese fields :

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

I am looking for a way to execute a database request to find all users that have a given string into their stringList, something like

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

But of course, this doesn't work. Is there any way to make it work ?

Edit :

Here are my actual class fields :

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

With this request :

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

I get that error :

JPAQueryException occured : Error while executing query SELECT u from User u where ? IN u.eventTopicIds: Syntax error in SQL statement "SELECT USER0_.ID AS ID3_, USER0_.EMAIL AS EMAIL3_, USER0_.FBID AS FBID3_, USER0_.FIRSTNAME AS FIRSTNAME3_, USER0_.GENDER AS GENDER3_, USER0_.GOOGLEACCESSTOKEN AS 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 (.[*])) "; expected "NOT, EXISTS, SELECT, FROM"; SQL statement: select user0_.i开发者_Python百科d as id3_, user0_.email as email3_, user0_.fbId as fbId3_, user0_.firstname as firstname3_, user0_.gender as gender3_, user0_.googleAccessToken as 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 (.)) [42001-149]


Normally you should be able to run a JPA query like

User.find("Select u from User as u inner join u.stringList as strings where ? in strings", "HelloWorld").fetch();

Did a small test, my object had emails instead of Strings, but it should be the same, unless you run into some reserved words. The problem was not the in keyword, I had to inner join the stringList in order to use the in keyword. A bit quick of the marker earlier:)

Hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消