Play Framework, OneToOne relationship not working

database, java, jpa, playframework, sql

Solution

I used both your models as you posted in the above link and tested successfully. As @Andrei mentioned in his comment the problem is not in mapping, it should be the way you saving them. Following are the code snippets i used for testing.

User

@Entity
public class User extends Model{

    / ..... fields as it is in your post

    public User(String user_id){
        this.userId = user_id;
    }

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

    public static User findByUserID(String user_id){

       /* Your models already in bi-directional relationship, so there is no need for external `fetch`, You can directly get `UserLegalName` object from `User`
               model if there is any match found on `UserLegalName` for the input.  */

       return find.where().eq("userId",user_id).findUnique();
    }
}

UserLegalName

@Entity
public class UserLegalName extends Model {

    / ..... fields as it is in your post

    public UserLegalName(User user_id, String first_name, String last_name){
        this.user = user_id;
        this.firstName = first_name;
        this.lastName = last_name;
    }
}

Controller

public class TestController extends Controller {

    public static Result insertUser(String user_id, String fname, String lname)
    {
        User user = new User(user_id);
        UserLegalName legal = new UserLegalName(user,fname,lname);
        user.legalName = legal;
        user.save();

        return ok(user.legalName.firstName);
    }

    public static Result getUser(String user_id)
    {
        User user = User.findByUserID(user_id);

        return ok(user.legalName.firstName);
    }
}

Routes

GET      /test/:userID/:fname/:lname       controllers.TestController.insertUser(userID : String, fname : String, lname : String)

GET      /user/:userID       controllers.TestController.getUser(userID : String)

Problem

I am using Play Framework(Java flavor) for a project. In this project I have two models which I would like to create a `OneToOne` relationship between. I have a `User` model and a `UserLegalName` model. I would like each `User` to have a `UserLegalName` model. `User` model code `UserLegalName` model code The issue is that the `User` and the `UserLegalName` do not seam to getting "related" The `user_user_id` column is always `NULL`. I have tried `JoinColumn(name = "user_id")` for the `User` in `UserLegalName` but this does not work either Edit: After taking @Sivakumar answer and fixing my code the `UserLegalName` is now storing correctly However when I attempt to get the `UserLegalName` for a user, it still turns up `null` ``` User.find.where().eq("userId", userId).findUnique() ``` Which returns ``` {"userId":"f5ea6d1d-d22d-4127-b6e7-0b3d0446edfe","legalName":null} ``` Edit 2: You can add `fetch=FetchType.EAGER` to the `OneToOne` annotation in the `User` model and that will fetch the `UserLegalName` every time. However in reality the `User` model is much more complicated. It holds many more relationships. Is there a different way to do this? By keeping the fetch type as `EAGER` it could create inefficient queries(EX: I only want the users email, in a separate table, but it also queries the `User_Legal_Name` table)

Original source