简体   繁体   English

休眠-从多个字段到同一实体的关系

[英]Hibernate - relationship from multiple fields to same entity

I've got entity FooDetails, which has two fields: Customer and list of Location. 我有实体FooDetails,它具有两个字段:客户和位置列表。 Customer has Address (@OneToOne unidirectional mapping), and Location also has Address with @OneToOne mapping. 客户具有地址(@OneToOne单向映射),而位置也具有具有@OneToOne映射的地址。

It happens that Address in Customer and in Location are the same. 碰巧“客户”和“位置”中的“地址”相同。 All these objects come from remote service and I manually put ID from remote object into entity before saving it. 所有这些对象都来自远程服务,在保存之前,我手动将远程对象的ID放入实体中。 Mapping looks like this: 映射如下所示:

@Entity
@Table(name = "FOO")
public class FooDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private long id;

    @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "customer_id")
    private Customer customer;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "details_id")
    private Set<Location> locationList;
...
}

@Entity
@Table(name = "CUSTOMER")
public class Customer {

    @Id
    @Column(name = "customer_id", unique = true)
    private long customerId;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "address_id")
    private Address address;
...
}

@Entity
@Table(name = "LOCATION")
public class Location {

    @Id
    @Column(name = "location_id", unique = true)
    private long locationId;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "address_id")
    private Address address;
...
}

@Entity
@Table(name = "ADDRESS")
public class Address{

    @Id
    @Column(name = "address_id")
    private long addressId;
    ...

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof StawareAddress)) return false;

        StawareAddress that = (StawareAddress) o;

        return addressId == that.addressId;
    }

    @Override
    public int hashCode() {
        return (int) (addressId ^ (addressId >>> 32));
    }
}

When I receive whole FooDetails object from webservice I try to save it to local database. 当我从Web服务接收整个FooDetails对象时,我尝试将其保存到本地数据库。

If database is clean (no Addresses saved yet), one Address with proper id from WS is saved. 如果数据库是干净的(尚未保存地址),则会保存一个来自WS且具有正确ID的地址。 If there already is an address with this id Hibernate tries to insert new one into database and it there is an error because of unique constraint on addressId. 如果已经有一个具有此ID的地址,则Hibernate会尝试将新的ID插入数据库,并且由于对addressId的唯一约束,将出现错误。

I'm using Spring Data Jpa for saving entities (save() method). 我正在使用Spring Data Jpa保存实体(save()方法)。

What obvious entity mapping problem I missed? 我错过了哪些明显的实体映射问题?

I would actually discourage the use of CascadeType.ALL here and manually handle it. 我实际上不鼓励在这里使用CascadeType.ALL并手动处理它。

public void saveFoodetails(FooDetails fooDetails) {
  Address address = addressRepository.find( fooDetails.getAddress().getId() );
  if ( address != null ) {
    // perhaps you update address with data from fooDetails.getAddress()
    addressRepository.save( address );
    // associate attached address instance with fooDetails now.
    fooDetails.setAddress( address );
  }
  else {
    // save the new incoming address contained in FooDetails
    addressRepository.save( fooDetails.getAddress() );
  }
  // now save/update FooDetails
  fooDetailsRepository.save( fooDetails );        
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM