如何在使用Hibernate进行延迟加载的情况下直接获取外键ID而不是整个实体? [英] How can i fetch foreign key id directly instead of entire entity in case of lazy loading using Hibernate?
问题描述
我的实体如下: 为了直接访问外键(id),Hibernate中有没有选择?
$ b $ pre $ @Entity
@Table(name =state)
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class,property =@id)
public class State实现java.io.Serializable {
private Integer id;
私人国家国家;
私人字符串名称;
@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;
$ p $在这个实体中而不是整个国家只是我想获取外键ID值...
code> @Column(name =COUNTRY_ID)
private long countryId;
My Entity is as follows:-
@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;
}
}
In this entity instead of Entire country just i want to fetch foreign key id value...
For accessing foreign key (id) directly, is there any option in Hibernate?
You can also add (depending on you id column name):
@Column(name = "COUNTRY_ID")
private Long countryId;
这篇关于如何在使用Hibernate进行延迟加载的情况下直接获取外键ID而不是整个实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!