i using play framework 2.0.4 , in model class 1 of variables array list<long>
datatype, declared here:
public list<long> associatebooks = new arraylist<long>();
i have view template invokes controller method 2 long values represent id's of 2 different objects of same model type.
get /addassociate/:oid/:id controllers.application.addassociate(oid: long, id: long)
the controller method invoked:
public static result addassociate(long oid, long id) { book.addassociate(oid, id); return redirect(routes.application.index()); }
i know controller method performing action because redirected index page , know receives values because url changes expected.
the problem lies in book model method addassociate shown below.
public static void addassociate(long oid, long match) { list<book> allbooks = new arraylist<book>(); allbooks = find.all(); for(book book: allbooks) { if(book.id == oid) { book.associatebooks.add(match); book.save(); } } }
in method want go through book objects , if id matches value of variable oid
passed view controller want add value of match (the other arguement) associatebooks
list (of type long
) belongs each journey object. try , save changes object being considered during iteration. problem list associatebooks
still seems empty when print out on other view templates.
update: if remove condition still doesn't add list.
if want save list of associated books, maybe follow this:
note: suggest create additional table named
book_associated
.+--------+-------------------+ |book_id |associated_book_id | +--------+-------------------+ |1 |2 | |1 |3 | +--------+-------------------+
it means book id
1
associated book id2
,3
but, has disadvantages if
book 1
associatedbook 2
cannot determine ifbook 2
associatedbook 1
because didn't declared on table.
so on model should below:
@entity @table(name = "book") public class book extends model { @id public long id; // other fields ... @manytomany @jointable(name="book_associated", joincolumns= @joincolumn(name="book_id", referencedcolumnname="id"), inversejoincolumns= @joincolumn(name="associated_book_id", referencedcolumnname="id") ) public list<book> associatebooks = new arraylist<>(); // other methods , finder ... }
here can save list of associated book of book. reference many-to-many relation on java :
Comments
Post a Comment