簡體   English   中英

在使用Hibernate進行延遲加載的情況下,如何直接獲取外鍵ID,而不是整個實體?

[英]How can i fetch foreign key id directly instead of entire entity in case of lazy loading using Hibernate?

我的實體如下:

@Entity
@Table(name = "state")
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id")
public class State implements java.io.Serializable {

private Integer id;
private Country Country;
private String name;

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
    return this.id;
}

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

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "country", nullable = false)
@JsonManagedReference(value="country-state")
public Country getCountry() {
    return this.country;
}

public void setCountry(Country country) {
    this.country = country;
 }

}

在這個實體而不是整個國家中,我只想獲取外鍵ID值...

為了直接訪問外鍵(id),Hibernate中是否有任何選項?

您還可以添加(取決於您的id列名稱):

@Column(name = "COUNTRY_ID")
private Long countryId;

一種解決方法是將虛擬屬性添加到您的實體,並將其設置為insertable = false, updatable = false

String countryKey;

@Column(name = "country",  insertable = false, updatable = false)
public String getCountryKey() {
    return this.countryKey;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM