java - JPA HIbernate - ManyToOne 映射 - 如果不存在则插入

标签 java hibernate jpa

我有以下 2 个类(实体)。

人类

@Entity
@Table(name = "person")
public class Person {

  @Id
  @GeneratedValue(strategy= GenerationType.SEQUENCE, 
  generator="person_id_seq")
  @SequenceGenerator(name="person_id_seq", sequenceName="person_id_seq", 
  allocationSize=1)
  private Integer person_id;

  @ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
  @JoinColumn(name = "location_id")
  private Location location;
}

位置类

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

  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "location_seq_gen")
  @SequenceGenerator(name = "location_seq_gen", sequenceName = "location_id_seq", allocationSize = 1)
  @Column(name = "location_id")
  private Long id;

  @Column(name = "address_1")
  private String address1;

  @Column(name = "address_2")
  private String address2;

  @Column(name = "city")
  private String city;

  @Column(name = "state")
  private String state;

  @Column(name = "zip")
  private String zipCode;

  @Column(name = "location_source_value")
  private String locationSourceValue;

public Location() {
}

public Location(String address1, String address2, String city, String state, String zipCode) {
    this.address1 = address1;
    this.address2 = address2;
    this.city = city;
    this.state = state;
    this.zipCode = zipCode;
}

public Long getId() {
    return id;
}

public Long getId(String address1, String address2, String city, String state, String zipCode){
    return this.id;
}

public void setId(Long id) {
    this.id = id;
}

public String getAddress1() {
    return address1;
}

public void setAddress1(String address1) {
    this.address1 = address1;
}

public String getAddress2() {
    return address2;
}

public void setAddress2(String address2) {
    this.address2 = address2;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

public String getState() {
    return state;
}

public void setState(String state) {
    this.state = state;
}

public String getZipCode() {
    return zipCode;
}

public void setZipCode(String zipCode) {
    this.zipCode = zipCode;
}

public String getLocationSourceValue() {
    return locationSourceValue;
}

public void setLocationSourceValue(String locationSourceValue) {
    this.locationSourceValue = locationSourceValue;
}

我希望能够做的是以下内容。

  • 当我插入一条新的 Person 记录时,我将提供 addressLine1、addressLine2、城市、州、邮政编码,如果记录存在,它应该检查 Location 表。如果存在,则从 Location 表中获取 location_id,并插入具有现有 location_id 的新 Person 记录。如果不存在,则在 Location 表中创建一条新记录,获取 location_id 并将其用作新 Person 记录的 location_id。

我相信这可以通过适当的 JPA Hibernate 注释来实现。

目前,每当我插入一条新的 Person 记录时,即使 Location 存在,它也会在 Location 表中创建一条新记录。

请帮忙。提前致谢!

最佳答案

您是否覆盖了 equals 和 hashCode 方法?通过这种方法,您将添加标识表中的每一行。您已正确指定注释,但 Hibernate 无法确定此行是否存在。 Hibernate 内部使用 Map,因此 equals 和 hashCode 应该可以解决您的问题。

关于java - JPA HIbernate - ManyToOne 映射 - 如果不存在则插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44528539/

相关文章:

java - 关于 hibernate 标准+限制的错误LazyInitializationException(Spring)

java - Hibernate的mappedBy和ManyToMany

java - 是否可以在包含目标+连接字段的子类上使用@JoinTable?

java - 如何使用 Jackson 解析对象数组?

Java 正则表达式行为

java - 添加到检索到的 ArrayList

java - 存在子查询的 JPA 规范使 SQL Server 语法失败

java - 移动 (Android) 游戏在线和互动的最佳实践/最佳服务

hibernate - 通用 DAO 模式实现设计

java - Hibernate 列名称中的特殊字符