[2.2.1 - Java] Datasource user is null ? updating User model (to-do list enhanced)

1,136 views
Skip to first unread message

Tristan Pierquet

unread,
Feb 11, 2014, 10:36:42 AM2/11/14
to play-fr...@googlegroups.com
Hello everyone, 

As my problem has changed, I am making a new topic.

So here's the point : I am trying to enhance the to-do list tutorial. I made a login page (which works well), and something like permission system. But now I'd like to allow people to change their password/email, aaaaaaand it's gone.
I made a small inner class in my Application called UpdateUser containing only the required fields. I made my update function :
public static String update(String id, User user) {
                        user
.update(id);
                       
return ("Your profile has been updated");
               
}



The message is meant to be in my flash. And of course, I made my function in my controller :

public static Result updateUser()
{
Form<UpdateUser> filledForm = updateUserForm.bindFromRequest();
if(filledForm.hasErrors())
{
flash("success","Error while updating (Form filled uncorrectly)");
}else{
UpdateUser uuser = filledForm.get();
if(uuser.id.equals(User.find.byId(request().username()).id))
{
User newUser = new User(uuser.id, uuser.email, User.find.byId(request().username()).name, User.find.byId(request().username()).surname, User.find.byId(request().username()).rank, uuser.password);
System.out.println(newUser.toString());
flash("success", User.update(newUser.id, newUser));
}else{
flash("success", "Error while updating (ID not corresponding)");
}
}
return redirect(routes.Application.dashboard());
}

And when I submit the form, what I get is : 
[RuntimeException: DataSource user is null?]

Sooooo... I'm getting quite pissed as I'm stuck on this since yesterday. Every thread I found that seemed related told me to uncomment the ebean.default line, but it is already. 
I really don't get it. Did I forgot something in my conf file ? Should I just give up and go back to elementary school ?

JavaPlayer

unread,
Feb 11, 2014, 11:52:59 AM2/11/14
to play-fr...@googlegroups.com
Did you uncomment the line for the ebean configuration in application.conf ?

 db.default.driver=org.h2.Driver
 db.default.url="jdbc:h2:mem:play"
 db.default.user=sa
 db.default.password=""

and also: 
ebean.default="models.*"


In build.sbt do you add the jdbc and dependencies to libraryDependencies
    javaJdbc,
    javaEbean,

JavaPlayer

unread,
Feb 11, 2014, 11:53:44 AM2/11/14
to play-fr...@googlegroups.com
Also, try to restart play after that, sometime it doesn't reload.

Tristan Pierquet

unread,
Feb 11, 2014, 11:58:02 AM2/11/14
to play-fr...@googlegroups.com
First of all, thanks for your answer.

The ebean line was already uncommented, so did the lines concerning the db (excepted user and password).
concerning the build.sbt file, here is what I've added :

libraryDependencies ++= Seq(
  javaJdbc,
  javaEbean,
  cache
)     

Is it good ? 

JavaPlayer

unread,
Feb 11, 2014, 1:15:51 PM2/11/14
to play-fr...@googlegroups.com
Yes

Tristan Pierquet

unread,
Feb 12, 2014, 4:11:09 AM2/12/14
to play-fr...@googlegroups.com
Hello again, 

Well, despite your help, it still doesn't work. Yet I still get the RuntimeException : Datasource user is null? error. What's weird is that this particular function seems to disconnect the user (or maybe it's the exception ?) since when I try to go back to the index (assuming I'm logged in, this should get me to the task page), I'm redirected to the login page.

What's even weirder is that I first made a form using all of the fields of the User class, and it worked. May it be a problem caused by the cast from the "UpdateUser" class (which only contains ID, email & passphrase) to the User class (containing also first/last name, privilege level and so on) ? Yet I use the data contained in the session (I get the user in every page I make)...

Tristan Pierquet

unread,
Feb 12, 2014, 4:44:56 AM2/12/14
to play-fr...@googlegroups.com
Oops, sorry. It doesn't disconnect the user, it's just that I made a mess in my pages (you can log in without logging out).

JavaPlayer

unread,
Feb 12, 2014, 8:55:01 AM2/12/14
to play-fr...@googlegroups.com
Can you show us your User Class ? 
Message has been deleted

Tristan Pierquet

unread,
Feb 12, 2014, 9:59:47 AM2/12/14
to play-fr...@googlegroups.com
Yes, of course.

package models;

import java.util.List;

import javax.persistence.*;

import controllers.Application;
import controllers.Application.UpdateUser;

import play.data.validation.Constraints.Required;
import play.db.ebean.Model;
import play.db.ebean.Model.Finder;

@Entity
public class User extends Model {
@Id
@Required
public String id;
public String email;
@Required
public String name;
public String surname;
@Required
public int rank;
@Required
public String passphrase;
public static Finder<String,User> find = new Finder(String.class, User.class);
/**
* @param id
* @param email
* @param name
* @param surname
* @param passphrase
* @param rank
*/
public User(String id, String email, String name, String surname, int rank,
String passphrase) {
this.id = id;
this.email = email;
this.name = name;
this.surname = surname;
this.rank=rank;
this.passphrase = passphrase;
}

public static List<User> all() {
return find.all();
}
public static String create(User user) {
try{
user.save();
return ("User "+user.id+" has been successfully created");
}catch(Exception e){return("Error : user "+user.id+" already exists");}
}
public static void deleteUser(String id) {
find.ref(id).delete();
}
public static String update(String id, User user) {
user.update((Object)id);
return ("Your profile has been updated");
}

public static User authenticate(String id, String passphrase) {
return find.where().eq("id", id)
           .eq("passphrase", passphrase).findUnique();
}
public String toString()
{
return("User : "+this.id+" ; "+this.email+" ; "+this.surname+" "+this.name+" ; rank"+this.rank+" ; passphrase : "+this.passphrase);
}
}



Here you are. But in fact, I found the problem and eventually solved it, but my message got deleted. I just had to cast the "id" parameter into an Object.

JavaPlayer

unread,
Feb 12, 2014, 10:08:28 AM2/12/14
to play-fr...@googlegroups.com
Good


On Tuesday, February 11, 2014 10:36:42 AM UTC-5, Tristan Pierquet wrote:
Reply all
Reply to author
Forward
0 new messages