java - Play 2.2.2 ArrayLists return null when trying to access -
i have play-2.2.2 installed having trouble ebean @onetomany relationships when trying access arraylist.
i have user class contains arraylist of address class. code follows:
@entity @inheritance(strategy= inheritancetype.single_table) @discriminatorcolumn(name="type", discriminatortype = discriminatortype.string) public class user extends model{ @id private long id; @onetomany(cascade=cascadetype.all) private arraylist<address> addresslist = new arraylist<address>(); ... public void addadress(address address){ this.addresslist.add(address); }
and class address
@entity public class address extends model { @id private long id; @manytoone private user user; ...
now lets make new user , new address. want add address new user's arraylist , save them. bit this:
user newuser = new user(); address newaddress = new address(); newuser.addaddress(newaddress); newuser.save();
the problem code address not saved. shouldn't saved since specified cascadetype all?
another issue of time, users created before address. lets have user. i'm trying add new address existing user:
user user = user.getcurrentuser(); user.addaddress(new address()); user.save();
this code gives me error, stating nullpointerexception in method addaddress. says addresslist null. why that? can't figure out. appreciated.
this should because jpa annotations not correctly assigned.
you should tell ebean how fetch address adding lookup info, otherwise doesn't know how that:
@onetomany(cascade=cascadetype.all, mappedby = "id") // id field name in address private set<address> addresslist;
also should use set it's recommended: http://www.avaje.org/manydatatypes.html
the reason preferring set not on top of head recall lists cause problems when multiple fetch joins.
and in address class:
@manytoone @joincolumn(name = "(the id column name in user table)") private user user;
Comments
Post a Comment