当我只访问外键ID时,如何防止Hibernate获取连接的实体? [英] How can I prevent Hibernate fetching joined entities when I access only the foreign key id?

查看:81
本文介绍了当我只访问外键ID时,如何防止Hibernate获取连接的实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Hibernate实体 Parent ,它与另一个: Child 连接。在数据库中,我有一个列 parent.child_id ,它在 child.id 上有一个外键约束。假设我延迟加载,当我加载一个父母的实例时,我希望能够访问 parent.getChild()。getId(),而不会在表中查找。



code> parent.getChild()能够完成对 Child.getId()的请求,因为它已经加载了孩子的来自 parent.child_id 列的ID。但是,我在表上看到了另一个查询。



为什么需要额外的查询,以及如何我避开它?在这种情况下,我只对外键ID感兴趣,并且不希望加载整行。



类:

  class父类{
@Id @Column(name =id,unique = true,nullable = false)
private Integer id;

@ManyToOne(fetch = FetchType.LAZY)@JoinColumn(name =child_id)
私人子女;

$ b class Child {
@Id @Column(name =id,unique = true,nullable = false)
private Integer id;

@Column(name =name)
私有字符串名称;
}

表格

  CREATE TABLE child(
id int PRIMARY KEY,
name varchar(10)
);

CREATE TABLE parent(
id int PRIMARY KEY,
child_id int REFERENCES(child.id)
);


解决方案

这是由 Child 使用字段访问(因为注释放在字段上),因此Hibernate在调用它的任何方法时只是初始化代理。





从JPA 2.0(Hibernate 3.5)开始,您可以用细粒度的方式对其进行配置:

  @Access(AccessType.FIELD)//默认为字段访问$ b $ class class Child {
private Integer id;

@Column(name =name)
私有字符串名称;

@Access(AccessType.PROPERTY)//使用属性访问id
@Id @Column(name =id,unique = true,nullable = false)
public整数getId(){...}

...
}


I have a Hibernate entity Parent that is joined to another: Child. In the database I have a column parent.child_id that has a foreign key constraint on child.id. Assuming I'm lazily loading, when I load an instance of Parent I expect to be able to access parent.getChild().getId() without incurring a lookup on the child table.

I expected the proxy returned by parent.getChild() to be able to fulfil a request to Child.getId() as it has already loaded the child's id from parent.child_id column. However, I see an additional query on the child table.

Why is this extra query necessary, and how can I avoid it? In this instance I'm only interested in the foreign key ID, and do not wish to load the whole row.

Classes:

class Parent {
  @Id @Column(name = "id", unique = true, nullable = false)
  private Integer id;

  @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "child_id")
  private Child child;
}

class Child {
  @Id @Column(name = "id", unique = true, nullable = false)
  private Integer id;

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

Tables:

CREATE TABLE child (
    id int PRIMARY KEY,
    name varchar(10)
);

CREATE TABLE parent (
    id int PRIMARY KEY,
    child_id int REFERENCES (child.id)
);

解决方案

It's caused by the fact that Child uses field access (since annotations are placed on fields), therefore Hibernate simply initializes the proxy when you call any of its methods.

If you move annotations to properies, it would work as expected.

Since JPA 2.0 (Hibernate 3.5) you can configure it in fine-grained way:

@Access(AccessType.FIELD) // Default is field access
class Child {
    private Integer id;

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

    @Access(AccessType.PROPERTY) // Use property access for id
    @Id @Column(name = "id", unique = true, nullable = false)
    public Integer getId() { ... }

    ...   
}

这篇关于当我只访问外键ID时,如何防止Hibernate获取连接的实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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