[英]Hibernate one-to-one mapping on another entity acting as primary key
So consider the following 2 tables: 因此,请考虑以下2个表:
table: User 表:用户
id (pk)
...
and table UserProfile: 和表UserProfile:
UserProfile
user_id(pk, and fk from User.id. fk is named profile_user_fk)
...
given this tables, I have the entity classes: 给定此表,我有实体类:
@Entity
@Table(name="User")
public class User implements Serializable {
private int id;
private UserProfile profile;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false, unique = true)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@OneToOne(mappedBy = "user")
public UserProfile getProfie() {
return profile;
}
public void setProfile(UserProfile p) {
profile = p;
}
...
}
And the User class:
@Entity
@Table(name="UserProfile")
public class UserProfile implements Serializable {
private User user;
@OneToOne
@PrimaryKeyJoinColumn(name="profile_user_fk")
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
...
}
I don't want to add an extra column in UserProfile because I think this column is meaningless. 我不想在UserProfile中添加额外的列,因为我认为此列毫无意义。 User.id should be sufficient to indicate the identity of UserProfile records. User.id应该足以表明UserProfile记录的身份。
So I supposed the @OneToOne annotations will tell hibernate user is a pk and also fk and should refer to User.id for its own id; 因此,我认为@OneToOne批注将告诉休眠用户是pk还是fk,并应参考User.id作为其自身的ID; however executing the code shows: 但是执行代码显示:
org.hibernate.AnnotationException: No identifier specified for entity: xxx.UserProfile
Apparently what I thought was wrong - but I've no idea what can I do to fix it without altering the schema. 显然我认为是错的-但我不知道在不更改架构的情况下可以如何解决。
Any helps please. 任何帮助请。 Thanks! 谢谢!
The error 错误
No identifier specified for entity: xxx.UserProfile
says 说
In your Entity class (UserProfile), you have not defined a primary key. You must specify
either @Id annotation or an @EmbeddedId annotation. Bcoz, every class defined as Entity
with @Entity annotation, needs an @Id or @EmbeddedId property.
Modify your UserProfile class as below :- 如下修改您的UserProfile类:
@Entity
@Table(name="UserProfile")
public class UserProfile implements Serializable {
private long uProfileId;
private User user;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "uProfileId", nullable = false, unique = true)
public long getUProfileId() {
return uProfileId;
}
public void setUProfileId(long uProfileId) {
this.uProfileId = uProfileId;
}
@OneToOne
@PrimaryKeyJoinColumn(name="profile_user_fk")
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
...
}
Read: @Entity & @Id and @GeneratedValue Annotations 阅读: @Entity&@Id和@GeneratedValue注释
Entity Class without Primary Key : 没有主键的实体类:
If you don't want to add primary key in UserProfile table then you can use @Embedded & @Embeddable annotations or <component>
tag in xml mapping. 如果您不想在UserProfile表中添加主键,则可以在XML映射中使用@Embedded&@Embeddable批注或<component>
标记。 For more explanation on this look at below posts:- 有关此内容的更多说明,请参见以下帖子:-
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.