我正在努力在我的播放框架應用程序上成功實現OneToOne映射。OneToOne映射 - 播放框架Enitity
例子,我有如下:
@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: No identifier specified for 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
..不知道如何糾正?
謝謝
我也建議從模型擴展這兩個類,在這種情況下,你不再需要聲明ID字段,因爲模型超類會爲你做這個工作。 –