【问题标题】:Hibernate one-to-one mapping on another entity acting as primary key在另一个充当主键的实体上休眠一对一映射
【发布时间】:2014-11-06 06:13:22
【问题描述】:

因此考虑以下 2 个表格:

表:用户

id (pk)
...

和表 UserProfile:

UserProfile
user_id(pk, and fk from User.id. fk is named profile_user_fk)
...

鉴于这些表,我有实体类:

@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;
    }
...
}

我不想在 UserProfile 中添加额外的列,因为我认为该列没有意义。 User.id 应该足以表明 UserProfile 记录的身份。

所以我认为@OneToOne 注释会告诉休眠用户是一个 pk 也是 fk 并且应该引用 User.id 作为它自己的 id;但是执行代码显示:

org.hibernate.AnnotationException: No identifier specified for entity: xxx.UserProfile

显然我认为是错误的 - 但我不知道我可以做些什么来在不改变架构的情况下修复它。

请提供任何帮助。谢谢!

【问题讨论】:

  • 错误本身说org.hibernate.AnnotationException: No identifier specified for entity: xxx.UserProfile你需要在UserProfile类中添加主键列。

标签: java hibernate


【解决方案1】:

错误

No identifier specified for entity: xxx.UserProfile

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.

修改你的 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;
    }
    ...
}

阅读: @Entity & @Id and @GeneratedValue Annotations


没有主键的实体类:

如果您不想在 UserProfile 表中添加主键,则可以使用 @Embedded 和 @Embeddable 注释<component> xml 映射中的标记。有关这方面的更多解释,请查看以下帖子:-

  1. Using <component> or @Embedded & @Embeddable annotations
  2. Hibernate and no PK
  3. Hibernate/persistence without @Id

【讨论】:

  • 所以这意味着我还必须在 UserProfile 中添加一列,我认为这没用但只是为了遵守 Hibnerate?正如我所说,对 user.id 的 fk 也应该足以作为 UserProfile 的 pk。这是一种在不更改数据库架构的情况下解决问题的方法吗?谢谢!
  • 既然你已经创建了UserProfile类为Entity类,你必须在其中添加Id。如果您不想更改您的数据库表,则可以使用 @Embedded@Embeddable 注释或 <component> 标记以防万一与 xml 文件的映射。请参阅我的更新答案。
猜你喜欢
  • 2011-01-15
  • 2021-11-11
  • 2015-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-06
相关资源
最近更新 更多