2017-06-13 48 views
1

我有以下2個類(實體)。JPA HIbernate - ManyToOne映射 - 插入如果不存在

Person類

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

}

我希望能夠做的是以下幾點。

  • 當我插入新的人員記錄時,我將提供addressLine1,addressLine2,城市,州,郵政編碼,並且它應該檢查位置表中是否存在記錄。如果它存在,則從Location表中獲取location_id,並使用現有的location_id插入新的Person記錄。如果它不存在,請在Location表中創建一條新記錄,獲取location_id並將其用作新Person記錄的location_id。

我相信這可以通過適當的JPA Hibernate註釋來實現。

目前,無論何時我插入新的人員記錄,即使位置存在,它也會在位置表中創建新記錄。

請幫忙。提前致謝!

回答

0

你是否覆蓋了equals和hashCode方法?通過這種方法,您將添加標識表中的每一行。您已經正確指定了註釋,但Hibernate無法確定該行是否存在。內部Hibernate使用Map,所以equals和hashCode應該可以解決你的問題。

+0

謝謝你的回覆。請你可以用一個例子來說明這一點。 – coder1416

+0

請看這個鏈接 https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/persistent-classes.html#persistent-classes-equalshashcode 在這裏你可以找到你爲什麼需要重寫這些方法和示例。 –

+1

@Sergiy Rezvan我面臨同樣的問題,但我不清楚明白什麼是指定。請詳細說明應在實體表中進行哪些更改。 – eccentricCoder