本文介绍了Hibernate映射 - 使用关联表连接两个表 - 但有一个转折点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下实体... code>



客户:

  {
private String cust_id; //主键
.....

@OneToOne
地址地址; //我想加载客户的地址。
//可以将关联更改为@OneToMany,如果需要的话
}

Card:

  {
private String card_id; //主键
private String cust_id; //外键Customer.cust_id
....
}

地址:

  {
private String card_id; // card.card_id的外键
private String address1;
.....
}

当我加载客户时,我想加载具有关联表卡片的地址。但棘手的部分是地址没有主键。它只有一个外键。

解决方案

您可以在@OneToOne之后使用注释@JoinTable指向卡片的表,你不需要卡片实体,但是如果卡片的表格不仅仅是关系表格,你可以将卡片作为@OneToOne映射到用户,并且有一个@Transient'getAddress()'方法返回'this.card。 getAddress()',但在卡的实体上,您必须映射地址和卡之间的关系(@OneToOne(mappedBy ='card_id')),并且在Address中,可以将card_id映射为@Id。

第一选择



顾客:

 ($ name =cust_id),
inverseJoinColumns = @JoinColumn b $ b私人地址;

第二种选择

顾客:

  @OneToOne(mappedBy =cust_id)
私人卡片卡;

...
@Transient
public Address getAddress(){
return this.card == null? null:this.card.getAddress();

卡片:

  @OneToOne(mappedBy =card_id)
私有地址;

地址:

  @Id 
private String card_id;

在第二种情况下,卡片有一个由两个fks(Customer和Address)组成的Embedded pk



卡片:

  @EmbeddedId $ b $ private CustomerAddressPK id; 

CustomerAddressPK

  @Embeddable 
public class CustomerAddressPK(){

private String cust_id;
私人字符串card_id;
}


I have the following entities... Customer, Card and Address

Customer:

 {
     private String cust_id; // Primary key
     .....

     @OneToOne
     Address address; // I want to load address of the customer. 
                      // Can change the association to @OneToMany, if needed
 }

Card:

 {
    private String card_id; // Primary Key
    private String cust_id; // Foreign Key to Customer.cust_id
    ....
 }

Address:

 {
    private String card_id; // Foreign key to card.card_id
    private String address1;
    .....
 }

When I load customers I want to load Addresses with association table Card. But the tricky part is Address does not have a primary key. It only has a Foreign key.

解决方案

You could use the annotation @JoinTable after the @OneToOne to point the card's table, so you won't need an entity for card, but if the card's table isn't just a relational table, you could map card in User as @OneToOne and have a @Transient 'getAddress()' method that returns 'this.card.getAddress()', but on card's entity you must map the relation between Address and Card(@OneToOne(mappedBy='card_id')), and in Address you could map card_id as @Id.

First Alternative

Customer:

@OneToOne
@JoinTable(name="card", joinColumns = @JoinColumn(name="cust_id"),
inverseJoinColumns = @JoinColumn(name="card_id"))
private Address address;

Second alternative

Customer:

@OneToOne(mappedBy="cust_id")
private Card card;

...
@Transient
public Address getAddress(){
 return this.card == null ? null : this.card.getAddress();
}

Card:

@OneToOne(mappedBy="card_id")
private Address address;

Address:

@Id
private String card_id;

In the Second case Card has a Embedded pk which is formed by two fks(Customer and Address)

Card:

@EmbeddedId
private CustomerAddressPK id;

CustomerAddressPK

@Embeddable
public class CustomerAddressPK(){

  private String cust_id;
  private String card_id;
}

这篇关于Hibernate映射 - 使用关联表连接两个表 - 但有一个转折点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 10:36