如何仅检索关联的

如何仅检索关联的

本文介绍了如何仅检索关联的 ID 而不是实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的类:

I have a class that looks something like this:

@Entity
public class EdgeInnovation {
    @Id
    public long id;
    @ManyToOne
    public NodeInnovation destination;
    @ManyToOne
    public NodeInnovation origin;
}

还有一个看起来像这样:

and another one that looks something like this:

@Entity
public class NodeInnovation {
    @Id
    public long id;
    @OneToOne
    public EdgeInnovation replacedEdge;
}

因此每个表都映射到另一个表,因此一个实体将引用其他实体,这些实体将引用更多实体,依此类推,最终将从数据库中获取许多实体.有没有办法只获取键的值(整数/长)而不是它所指的实体?像这样:

and so each table map to the other, so one entity will refer to other entities that will refer to more entities and so on, so that in the end there will be many entities that will be fetched from the database. Is there any way to only get the value (integer/long) of the key and not the entity it refers to? something like this:

@ManyToOne(referToThisTable="NodeInnovation")
@Entity
public class EdgeInnovation {
    @Id
    public long id;
    @ManyToOne(referToTable="NodeInnovation")
    public Long destination;
    @ManyToOne(referToTable="NodeInnovation")
    public Long origin;
}

@Entity
public class NodeInnovation {
    @Id
    public long id;
    @OneToOne(referToTable="EdgeInnovation")
    public Long replacedEdge;
}

这是一个例子.我想要绿色的东西,我得到所有红色的东西.这浪费了从磁盘读取的内存和时间.

Here's an example. I want the stuff in green, I get all the stuff in red along with it. This wastes memory and time reading from disk.

推荐答案

您只需将外键映射为基本映射而不是关系:

You would just map the foreign keys as basic mappings instead of Relationships:

@Entity
public class EdgeInnovation {
    @Id
    public long id;
    @Column(name="DESTINATION_ID")
    public Long destination;
    @Column(name="ORIGIN_ID")
    public Long origin;
}

或者您可以同时访问 EdgeInnovation 中的 ID 和引用实体,但您需要决定要使用哪个来设置映射:

Or you can have access to both the ID and the referenced entity within EdgeInnovation, but you'll need to decide which you want to use to set the mapping:

@Entity
public class EdgeInnovation {
    @Id
    public long id;
    @Column(name="DESTINATION_ID", updatable=false, insertable=false)
    public Long destination_id;
    @ManyToOne
    public NodeInnovation destination;
    @Column(name="ORIGIN_ID", updatable=false, insertable=false)
    public Long origin_id;
    @ManyToOne
    public NodeInnovation origin;
}

在上面的例子中, origin_id 是只读的,而 origin 引用用于设置表中的外键.但是,应该对这两个字段进行任何更改,以保持对象映射彼此同步.

In the above example, the origin_id is read-only while the origin reference is used to set the foreign key in the table. Any changes though should be made to both fields to keep the object mappings in synch with each other.

另一种选择是使用提供者的本机代码来查找引用是否是惰性的且未被触发,然后获取外键值.如果它已被触发,您可以只使用引用来获取 ID 值,因为它不会导致查询获取任何内容.不过,您必须查看 EclipseLink 的源代码.

Another alternative is to use the provider's native code to find if the reference is lazy and wasn't triggered, and then get the foreign key value. If it has been triggered, you can just use the reference to get the ID value, since it won't cause a query to fetch anything. This is something you would have to look into EclipseLink's source code for though.

这篇关于如何仅检索关联的 ID 而不是实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 13:32