如何在使用Hibernate进行延迟加载的情况下直接获取外键ID而不是整个实体? [英] How can i fetch foreign key id directly instead of entire entity in case of lazy loading using Hibernate?

查看:86
本文介绍了如何在使用Hibernate进行延迟加载的情况下直接获取外键ID而不是整个实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的实体如下:
$ 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值...

为了直接访问外键(id),Hibernate中有没有选择?

$ b $您还可以添加(取决于您的ID列名称): $ b

  

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆