Hibernate 外键作为主键的一部分

发布于 2024-11-30 12:59:19 字数 342 浏览 1 评论 0原文


我必须使用 Hibernate,但不太确定如何解决这个问题,我有 2 个具有 1..n 关系的表,如下所示:

-------
TABLE_A
-------
first_id (pk)
second_id (pk)
[other fields]

-------
TABLE_B
-------
first_id (pk)(fk TABLE_A.first_id)
second_id (pk)(fk TABLE_A.second_id)
third_id (pk)
[other fields]

我如何使用 Hibernate 来管理这个问题???

我不知道如何管理第二个表的主键......

I have to work with hibernate and not very sure how solve this problem, I've 2 table with a 1..n relationship like this:

-------
TABLE_A
-------
first_id (pk)
second_id (pk)
[other fields]

-------
TABLE_B
-------
first_id (pk)(fk TABLE_A.first_id)
second_id (pk)(fk TABLE_A.second_id)
third_id (pk)
[other fields]

How can I manage this with Hibernate???

I don't have idea how to manage the primary key for the second table...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

天气好吗我好吗 2024-12-07 12:59:19

Hibernate 参考文档<中有一个与您的情况完全相似的示例/a>.就在这个示例之前,您会找到解释。下面是与您的问题相匹配的示例(用户是表 A,客户是表 B):

@Entity
class Customer {
   @EmbeddedId CustomerId id;
   boolean preferredCustomer;

   @MapsId("userId")
   @JoinColumns({
      @JoinColumn(name="userfirstname_fk", referencedColumnName="firstName"),
      @JoinColumn(name="userlastname_fk", referencedColumnName="lastName")
   })
   @OneToOne User user;
}

@Embeddable
class CustomerId implements Serializable {
   UserId userId;
   String customerNumber;

   //implements equals and hashCode
}

@Entity 
class User {
   @EmbeddedId UserId id;
   Integer age;
}

@Embeddable
class UserId implements Serializable {
   String firstName;
   String lastName;

   //implements equals and hashCode
}

注意:如果您为这两个表提供了代理标识符,则会简单得多。除非您被迫处理遗留模式,否则请帮自己一个忙并使用代理键。

There is an example which is completely similar to your case in the Hibernate reference documentation. Just before this example, you'll find the explanations. Here's the example, which matches your problem (User is table A, and Customer is table B):

@Entity
class Customer {
   @EmbeddedId CustomerId id;
   boolean preferredCustomer;

   @MapsId("userId")
   @JoinColumns({
      @JoinColumn(name="userfirstname_fk", referencedColumnName="firstName"),
      @JoinColumn(name="userlastname_fk", referencedColumnName="lastName")
   })
   @OneToOne User user;
}

@Embeddable
class CustomerId implements Serializable {
   UserId userId;
   String customerNumber;

   //implements equals and hashCode
}

@Entity 
class User {
   @EmbeddedId UserId id;
   Integer age;
}

@Embeddable
class UserId implements Serializable {
   String firstName;
   String lastName;

   //implements equals and hashCode
}

Note: it would be much much simpler of you had a surrogate identifier for those two tables. Unless you're forced to deal with a legacy schema, do yourself a favor and use surrogate keys.

耳根太软 2024-12-07 12:59:19

使用 @PrimaryKeyJoinColumn 和 @PrimaryKeyJoinColumns 注释。来自 Hibernate 手册

@PrimaryKeyJoinColumn 注释确实表示实体的主键用作关联实体的外键值。

Use @PrimaryKeyJoinColumn and @PrimaryKeyJoinColumns annotations. From Hibernate manual:

The @PrimaryKeyJoinColumn annotation does say that the primary key of the entity is used as the foreign key value to the associated entity.

伪装你 2024-12-07 12:59:19
public class User implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 5478661842746845130L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
}
@Entity
public class Author {

    @Id
    @Column(name = "AUTHOR_ID", nullable = false)
    private int authorId;
    @Column(name = "ENABLED", nullable = false, length = 1)
    private boolean enabled;

    @OneToOne
    @MapsId
    @JoinColumn(name = "AUTHOR_ID", referencedColumnName = "ID", nullable = false, insertable = false, updatable = false)
    User user;

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

}
public class User implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 5478661842746845130L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
}
@Entity
public class Author {

    @Id
    @Column(name = "AUTHOR_ID", nullable = false)
    private int authorId;
    @Column(name = "ENABLED", nullable = false, length = 1)
    private boolean enabled;

    @OneToOne
    @MapsId
    @JoinColumn(name = "AUTHOR_ID", referencedColumnName = "ID", nullable = false, insertable = false, updatable = false)
    User user;

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

}
回眸一笑 2024-12-07 12:59:19

对于表A,您可以使用embeddedId

@Emebddable
class PrimaryKeyIdTableA{
 private Integer firstId,

 private Integer secondId
}


class A {
 @EmebddedId
 private PrimaryKeyIdTableA id
}

对于表B,您也可以使用Idclass,如下所示

Class PrimaryKeyIdTableB {

private PrimaryKeyIdTableA id,

private Integer thirdId

 }

 @Idclass(PrimaryKeyIdTableB.class)
 Class TableB{
 @Id
 private Integer thirdId,
 
  @Id
  @onetoone
  @Joincolumns({
  @Joincolumn(name=firstid),
  @Joincolumn(name=secondId)})
 private PrimaryKeyIdTableA id
    }

For Table A you can make you of embeddedId

@Emebddable
class PrimaryKeyIdTableA{
 private Integer firstId,

 private Integer secondId
}


class A {
 @EmebddedId
 private PrimaryKeyIdTableA id
}

For Table B you can use Idclass also as shown below

Class PrimaryKeyIdTableB {

private PrimaryKeyIdTableA id,

private Integer thirdId

 }

 @Idclass(PrimaryKeyIdTableB.class)
 Class TableB{
 @Id
 private Integer thirdId,
 
  @Id
  @onetoone
  @Joincolumns({
  @Joincolumn(name=firstid),
  @Joincolumn(name=secondId)})
 private PrimaryKeyIdTableA id
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文