如何仅检索ID而不是关联的实体? [英] How do I retrieve only the ID instead of the Entity of an association?

查看:120
本文介绍了如何仅检索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而不是关联的实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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