Ebean ManyToMany: Model A has ManyToMany<Model B> . select count(*) B where B in A's List<B>

StackOverflow https://stackoverflow.com/questions/22140336

문제

The application allows users to select certain keywords (which we monitor the TwitterStream for)

Each Keyword contains a list of the tweet IDs that contain its keyword.

public class Keyword extends Model {

@Id
int id;
String keyword;

@ManyToMany
public List<Tweet> tweets = new ArrayList();
}

public class Tweet extends Model {

@Id
int id;
    TimeStamp datetime;
}

I'd like to get some data on how each keyword performs each day by select count(*), datetime grouping them by day. The following SQL query will accomplish what I need.

select datetime, count(*) from tweet t left outer join keyword_tweet on t.id=keyword_tweet.tweet_id group by cast(t.datetime as date) having t.datetime > '2014-02-02';

+---------------------+----------+
| datetime            | count(*) |
+---------------------+----------+
| 2014-02-02 13:27:45 |        1 |
| 2014-02-08 05:14:04 |        2 |
| 2014-02-09 08:34:31 |        1 |
| 2014-02-12 12:42:02 |        1 |
| 2014-02-13 06:00:09 |        2 |
| 2014-02-14 00:47:04 |        2 |
| 2014-02-15 07:26:30 |        6 |
| 2014-02-16 01:00:00 |       21 |
| 2014-02-17 00:06:50 |      916 |
| 2014-02-18 18:08:56 |        1 |
| 2014-02-19 01:28:40 |        1 |
| 2014-02-24 16:45:11 |        1 |
| 2014-02-26 14:43:54 |        4 |
| 2014-02-27 08:24:09 |        9 |
| 2014-02-28 05:08:16 |      411 |
+---------------------+----------+

How do I select * from Tweets where Tweet.id is IN Keyword.tweets ?

Also how in Ebean would I get a List that only contains Dates, and Count(*)?

Thanks guys!

도움이 되었습니까?

해결책

You can use something along these lines:

Keyword targetKeyword = Keyword.find.byId(1L);
List<Tweets> foundTweets = new ArrayList<Tweets>();
for(Tweet tw : Tweets.find.all()) {
    if(targetKeyword.tweets.contains(tw))
          foundTweets.add(tw);
}
return foundTweets;

This code will return all tweets contained in keyword id number 1. You can place this in a function passing the keyword id you want instead of 1L.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top