Note that there are some explanatory texts on larger screens.

plurals
  1. POJPA.em().find returns null for existing object
    primarykey
    data
    text
    <p>I have a problem with JPA in play framework 2.1. Here is my situation:</p> <p>I have action method which handles sign up for my application (User fills email and password and submits the form). In this method I check if the user exists in my database and if not I create new one. Here is simplified code which shows how it works:</p> <pre><code>public Result signUpSubmit(String email, String password) { User existingUser = (User) User.find("SELECT u FROM User u WHERE u.email=?", email).get(0); if (existingUser != null) { // code which handles existing user } else { User newUser = new User(email, password); Users.persistUserAsync(newUser); // calls JPA.em().persist(newUser) asynchronously // but I wait until the save is done // After this call I have new row in DB with newUser (with assigned id) System.out.println(newUser.id); // prints id which was assigned to new user in DB User u = (User)JPA.em().find(User.class, newUser.id) System.out.println(u.id); // throws NullPointer exception, because u is null } return renderJapid(); } </code></pre> <p>Can you tell me the reason why I get the null from the second find query?</p> <pre><code>public boolean persistUserAsync(User) { final ModelCreatingJob modelCreatingJob = new ModelCreatingJob(user); final Promise&lt;Boolean&gt; promisedSave = modelCreatingJob.now(); final Boolean saved = promisedSave.get(20000L); return saved; } </code></pre> <p>The ModelCreatingJob does only this:</p> <pre><code>return JPA.withTransaction(new F.Function0&lt;Boolean&gt;() { @Override public Boolean apply() throws UserLockedException, UserNotFoundException { return model.validateAndCreate(); } }); </code></pre> <p>Strange is that when I remove first find (just leave newUser = null) on second find I get valid user object.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload