JPA + Hibernate - 如何获取子实体的FK而无需获取该实体? [英] JPA + Hibernate - How to get FK of child entity without fetching that entity?

查看:94
本文介绍了JPA + Hibernate - 如何获取子实体的FK而无需获取该实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题可能的答案位于这里:
如何从JPA ManyToOne映射中检索外键而无需触击目标表?



然而,更好的解决方案(属性访问)在我的情况下不起作用(我缺少列异常 - 为什么?)

模型如下所示:实体小孩。表的列 child_id PK of child 表,所以它通常是 @ManyToOne 关系。



现在问题是,如果我获取实体,我需要访问 FK 值(aka。<$ c (code> Child entity)的$ c> PK 没有获取 Child 实体。我怎么做?



我使用注解,我的映射如下所示:

  @Entity 
@Table(name =parent)
public class Parent extends AbstractEntity {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name =patent_id,nullable = true)
私人子女;

@Column(name =child_id,insertable = false,updatable = false)
private Integer childId;

public Child getChild(){
return return patent;
}

public void setChild(Child child){
this.child = child;
}


public Integer getChildId(){
return childId;
}
}

我想要做的是调用 parent.getChild()。getId()不需要从数据库中额外提取 Child 实体。



根据我上面提到的答案,如果我将注释从字段移到getter(在实体中,我是否正确?),所请求的行为将是盒子外面。然而,当我将注释移动到getter时,我得到一个验证异常,它缺少 child 列(好奇,为什么 child 而不是 child_id )声明?)



PS: c $ c> FK 列作为单独的字段可以正常工作,但我不认为这是应该完成的。

解决方案

好了,阅读以下文章 http:// 256stuff .com / gray / docs / misc / hibernate_lazy_field_access_annotations.shtml
我已经意识到,该属性访问应该是我要获取的属性,而不是实际的对象。因此改变 id AbstractEntity 访问权限,就可以达到目的。


A possible answer to my question is located here: How can I retrieve the foreign key from a JPA ManyToOne mapping without hitting the target table?

However, the preferable solution (property access) does not work in my case (I got missing column exception - why?)

The model looks like this: Entities Parent and Child. Table parent has column child_id which is PK of child table so it is typical @ManyToOne relation.

Now the point is, if I fetch Parent entities, I need to have access to FK value (aka. PK of Child entity) without fetching Child entities. How can I do that?

I use Annotations and my mapping looks as follows:

@Entity
@Table(name = "parent")
public class Parent extends AbstractEntity {

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "patent_id", nullable = true)
private Child child;

@Column(name="child_id",insertable=false,updatable=false)
private Integer childId;

public Child getChild() {
    return patent;
}

public void setChild(Child child) {
    this.child = child;
}


public Integer getChildId(){
    return childId;
}
}

And what I want to do is call parent.getChild().getId() without extra fetches of Child entity from DB.

According to the answer I have mentioned above, If I moved annotations from field to getter (in Parent entity am I right?), requested behavior would be out of the box. However, when I move annotations to getter, I get a validation exception that child column is missing (curious, why child not child_id as declared?)

PS: Shown workaround to declare a FK column as separate field works fine, but I don't think that this is the way it should be done.

解决方案

OK, after reading following article http://256stuff.com/gray/docs/misc/hibernate_lazy_field_access_annotations.shtml I have realized, that property access should be to the property I want to fetch, not the actual child object. So changing id access of AbstractEntityfrom field to property, makes the trick.

这篇关于JPA + Hibernate - 如何获取子实体的FK而无需获取该实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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