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

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!

Answers


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.


Need Your Help

youtube api token upload video

c# youtube-api oauth-2.0 token

I am retriving a token through an website, then I save the token, and refresh token in a database. Then I am writing .net program that uploads video's located on our server to youtube. My problem i...

What is the scope of global variable in this case?

python python-3.x

I'm new to python, so this is probably a dumb question.