java - Play Framework 2.1 ebean construction -
i started using play framework 2.1.1 java developer.
got ebeans existing database , i'm building restful architecture serve , save beans restful client web application.
i have following beans definitions. feed core object of application. simplicity have not included getters , setters , many other beans, i'm focusing on ones give me troubles:
@entity public class feed extends model implements serializable { @id long feedid; ... ... ... @jsonmanagedreference("feed-item") @onetomany(fetch = fetchtype.lazy, mappedby = "feed") list<item> items; @jsonmanagedreference("feed-userfeed") @onetomany(fetch = fetchtype.lazy, mappedby = "feed") list<userfeed> userfeeds; } @entity public class item extends model implements serializable { @id long itemid; ... ... ... long urlid; @manytoone(fetch = fetchtype.lazy) @joincolumn(name = "url_id") url url; @formats.datetime(pattern = "yyyy-mm-ddthh:mm:ss") timestamp itempublishedat; @formats.datetime(pattern = "yyyy-mm-ddthh:mm:ss") timestamp itemupdatedat; @formats.datetime(pattern = "yyyy-mm-ddthh:mm:ss") timestamp createdat; @version timestamp updatedat; long feedid; @manytoone(fetch = fetchtype.lazy) @jsonbackreference("item-feed") @joincolumn(name = "feed_id") feed feed; long urlid; @manytoone(fetch = fetchtype.lazy) @joincolumn(name = "url_id") url url; @onetomany(fetch = fetchtype.lazy, mappedby = "item") @jsonmanagedreference("item-unseen") list<unseen> unseen; @onetomany(fetch = fetchtype.lazy, mappedby = "item") @jsonmanagedreference("item-score") list<score> scores; } @entity public class user extends model implements serializable { @id long userid; ... ... ... @onetomany @jsonmanagedreference("user-userfeed") list<userfeed> userfeeds; @onetomany @jsonmanagedreference("user-usertag") list<usertag> usertags; @onetomany @jsonmanagedreference("user-unseen") list<unseen> unseen; @onetomany @jsonmanagedreference("user-score") list<score> scores; } @entity public class score extends model implements serializable { @id long scoreid; ... ... ... long itemid; @manytoone(fetch=fetchtype.lazy) @jsonbackreference("score-item") @joincolumn(name = "item_id") item item; long userid; @manytoone(fetch=fetchtype.lazy) @jsonbackreference("score-user") @joincolumn(name = "user_id") user user; } here's feed controller:
import me.zenfeed.model.feed; import org.codehaus.jackson.jsonnode; import play.db.ebean.model; import play.libs.json; import play.mvc.bodyparser; import play.mvc.controller; import play.mvc.result; public class feedcontroller extends controller { public static result delete(long id) { new model.finder<>(long.class, feed.class).byid(id).delete(); return ok(); } /* return json of objects found*/ public static result get() { return ok(json.tojson(new model.finder<>(long.class, feed.class).fetch("userfeeds", new fetchconfig().query()).where().eq("userfeeds.userid", long.parselong(session("connecteduserid"))).findlist())); } /* accept json object save , return json new object*/ @bodyparser.of(bodyparser.json.class) public static result post() { jsonnode json = request().body().asjson(); feed f = json.fromjson(json, feed.class); f.save(); return ok(json.tojson(f)); } /* accept json object save , return json new object*/ @bodyparser.of(bodyparser.json.class) public static result put(long id) { jsonnode json = request().body().asjson(); feed f = json.fromjson(json, feed.class); f.update(id); return ok(json.tojson(f)); } } here's custom function use make query on db parameters:
list<item> l = new model.finder<>(long.class, item.class) .select("itemid, feedid, urlid, title, description, content, author, categories, itempublishedat, itemupdatedat, wordcount, fresh, vector, createdat, updatedat") .fetch("unseen", "itemid", new fetchconfig().query()) .fetch("scores", "score", new fetchconfig().query()) .fetch("feed.userfeeds", "userid", new fetchconfig().query()) .where() .eq("feed.userfeeds.userid", long.parselong(session("connecteduserid"))) .eq("feed.feedid", feedid) .orderby("scores.score") .findlist(); i have several problems with:
i understand saw wrong tutorial. argument @jsonmanagedreference , @jsonbackreference must same. feed-item relationship correct annotations @jsonmanagedreference("feed-item") , @jsonbackreference("feed-item").
- as can see tried format timestamp objects html5 format, no luck. long number. should make conversion date object? if yes where?
i went manual way generation of hashmap properties care of , gave json.tojson() method.
thank help.
Comments
Post a Comment