我正在努力在我的Play框架应用程序上成功实现OneToOne映射。
我有的例子是:@Entity
public class Profile extends GenericModel {
@Id
@GeneratedValue(generator = "foreignGenerator")
@GenericGenerator(name = "foreignGenerator", strategy = "foreign",
parameters = @Parameter(name = "property", value = "user"))
public static int id;
@OneToOne(mappedBy="profile", cascade = {CascadeType.ALL})
public static User user;
}
和:
@Entity
public class User extends Model {
@Required
public String firstName;
@Required
public String surname;
}
在这个设置中抛出:
org.hibernate.AnnotationException:没有为entity指定标识符:models.Profile
编辑:根据Christian Boariu's answer,我已根据您的建议和用户修改了个人资料:
@Entity
public class User extends GenericModel {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long user_id;
@Required
public String firstName;
@Required
public String surname;
@OneToOne(cascade = {CascadeType.ALL})
@PrimaryKeyJoinColumn(name = "user_id", referencedColumnName = "profile_id")
public Profile profile;
public Profile getProfile() {
return profile;
}
public void setProfile(Profile profile) {
this.profile = profile;
}
}
还为配置文件添加了getter / setter:
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
但我现在正在hibernate.id.IdentifierGenerationException: null id generated for:class models.Profile
...不确定如何纠正?
答案 0 :(得分:1)
问题在于你的Id定义。
不应为static
。
此外,用户也不应该是静态的。
<强>更新强>:
所以你的课应该是这样的:
@Entity
public class Profile extends GenericModel {
@Id
@GeneratedValue(generator = "foreignGenerator")
@GenericGenerator(name = "foreignGenerator", strategy = "foreign",
parameters = @Parameter(name = "property", value = "user"))
public int id;
@OneToOne(mappedBy="profile", cascade = {CascadeType.ALL})
public User user;
}
答案 1 :(得分:0)
固定。 suggesstions,如上所述修复了@OneToOne问题和hibernate.id.IdentifierGenerationException:为id:class models.Profile生成的null id是由于尝试持久化具有空id的实体 - 由于使用@primaryKeyJoin,所以更改为@JoinColumn