[Play 2.2.1 Java] ArrayList<String> in Model always null

37 views
Skip to first unread message

Tristan Pierquet

unread,
Apr 23, 2014, 8:59:26 AM4/23/14
to play-fr...@googlegroups.com
Hello everyone, 

I'm working on a simple application in which any user can be in charge of other Users. Thus, I created a field in my Model containing the email of those people, which is automatically updated when a new user is created. But this List is always null, even if I try to fill it before creating the User.

Here is the relevant code (my User class) :


-----------------------------------------------------------------------
package models;


//Import declarations here


@Entity
public class User extends Model {


     
@Id
     
@Required
     
public String email;
     
@Required
     
public String lastname;
     
public String firstname;
     
@Required
     
public String passphrase;
     
@Column(nullable=true)
     
public String superieur;
     
@Column(nullable=false)
     
public List<String> subordonnes;
     
public boolean admin;


     
public static Finder<String, User> find = new Finder<String, User>(String.class, User.class);


     
private final static Logger LOGGER = Logger.getLogger(User.class.getName());
     
public static final int EMAIL_SEARCH = 1;
     
public static final int NAME_SEARCH = 2;
 
     
public User(String lastname, String firstname, String passphrase, String email, String superieur, List<String> subordonnes) {
         
super();
         
this.lastname = lastname;
         
this.firstname = firstname;
         
this.passphrase = passphrase;
         
this.email = email;
         
this.superieur = superieur;
         
this.subordonnes = subordonnes;
     
}


     
public static List<User> all(){
         
return find.all();
     
}
 
     
public static String add(User u){
         
if(validateUser(u)){
             u
.subordonnes=new ArrayList<String>();
             u
.subordonnes.add("no...@none.com");
             u
.save();
             
if(u.superieur==null){
                 
return "User added";
             
}else{
                 
if(!(u.superieur.equals("no...@none.com"))){
                     
User x = User.find.byId(u.superieur);
                     
if(x.subordonnes==null){
                          LOGGER
.log(Log.VERBOSE, "List created (again ?)");
                          x
.subordonnes = new ArrayList<String>();
                     
}
                      x
.subordonnes.add(u.email);
                      x
.update((Object)x.email);
                      LOGGER
.log(Log.VERBOSE, "User "+u.email+" added to "+x.email+" list");
                 
}
             
return "User added";
             
}
         
}else{
             
return "Invalid data for user";
         
}
     
}
 
     
public static void remove(String e){
         find
.ref(e).delete();
     
}
 
     
public static String update(String id, User u){
         u
.update((Object)id);
         
return "Update successful";
     
}
 
 
     
public static boolean validateUser(User u){
         
Pattern pattern = Pattern.compile("[A-Z][a-z]+(-[A-Z][a-z]+)?");
         
Matcher matcher;
         
EmailValidator valid = EmailValidator.getInstance();
         
if(valid.isValid(u.email))
         
{
             
if(User.find.byId(u.email)==null){
                 matcher
= pattern.matcher((CharSequence)u.lastname);
                 
if(matcher.find())
                 
{
                     matcher
= pattern.matcher((CharSequence)u.firstname);
                     
if(matcher.find()){
                         pattern
=Pattern.compile(".{8,}");
                         matcher
= pattern.matcher((CharSequence)u.passphrase);
                         
if(matcher.find())
                         
{
                             
return true;
                         
}
                     
}
                 
}
             
}
         
}
         
return false;
     
}
 
     
public static User authenticate(String email, String passphrase) {
         
return find.where().eq("email", email)
           
.eq("passphrase", passphrase).findUnique();
     
}
 
     
public static List<User> search(String s, int i){
 
         
Pattern p = Pattern.compile(".*"+s+".*");
         
Matcher m;
         
List<User> elu = all();
         
List<User> ok = new ArrayList<User>();
         
for(User u : elu){
             
if(i==NAME_SEARCH){
                 m
= p.matcher((CharSequence)u.lastname);
             
}else{
                 m
= p.matcher((CharSequence)u.email);
             
}
             
if(m.find()){
                 ok
.add(u);
             
}
         
}
         
return ok;
     
}
 
}



I tried to use the method saveManyToManyAssociations("subordonnes"), but it resulted in a NullPointerException while testing.

What am I doing wrong ? (Amongst other things, of course...)

Thanks for your help.

Tristan Pierquet

unread,
Apr 24, 2014, 5:51:23 AM4/24/14
to play-fr...@googlegroups.com
Alright, I tested it again, and it still doesn't work. I tried this test : 

String jd = "jane...@whatever.com"
User u = new User("Doe", "Jane", "whatever", jd, "no...@none.com", new ArrayList<String>());
User.add(u);
assertNotNull(User.find.byId(jd));

Which pass, and this is normal. Then I make this one : 

u = new User("Doe", "John", "whatever", "john...@example.com", jd, new ArrayList<String>());
User.add(u);
assertNotNull(User.find.byId("john...@example.com"));

It succeeds. With the Logs I inserted in my User class, I can see that theorically, John Doe is inserted in Jane Doe's "subordonnes" List. But It's created again, even if I declared it as "new ArrayList<String>" when I created the User.

And finally, this test succeeds when it should fail miserably : 

User v = User.find.byId(jd);
assertNull(v.subordonnes);

I don't get it. Why isn't the List saved along with the whole user ? I tried modifying the password and the last name of John Doe, and it succeeded quite well. Is there some kind of voodoo ritual one has to do to save ArrayList in Model ?

Once again, thanks for any help.

amertum

unread,
Apr 25, 2014, 5:22:47 AM4/25/14
to play-fr...@googlegroups.com
A hint : http://stackoverflow.com/questions/287201/how-to-persist-a-property-of-type-liststring-in-jpa

The list of string you're trying to persist is an embedded value.
Reply all
Reply to author
Forward
0 new messages