有两种型号:
模型/User.java
@Entity
@Table(name="users")
public class User extends Model
{
@Id
public int user_id;
public String firstName;
public String lastName;
@OneToOne
@PrimaryKeyJoinColumn
public UserProfile profile;
public static Finder<Integer,User> find = new Finder<Integer,User>( Integer.class, User.class );
}
模型/UserProfile.java
@Entity
@Table(name="user_profiles")
public class UserProfile extends Model
{
@Id
public int user_id;
public String bio;
@OneToOne(mappedBy = "user_id")
public User user;
public static Finder<Integer,UserProfile> find = new Finder<Integer,UserProfile>( Integer.class, UserProfile.class );
}
一些数据:
INSERT INTO users VALUES(1,"Joe","Bloh");
INSERT INTO users VALUES(2,"Maria","Luis");
INSERT INTO user_profiles VALUES(1, "programmer");
INSERT INTO user_profiles VALUES(2, "tester");
以及从用户获取个人资料的相同代码:
User user = User.find.byId(2);
UserProfile profile = UserProfile.find.byId(1);
这会触发异常:
javax.persistence.PersistenceException: Error on models.UserProfile.user. mappedBy property [models.UserBad.user_id]is not a OneToOne?
两个模型如何在 Ebean ORM 中共享相同的主键,并具有 @OneToOne 关系?