How do I retrieve only the ID instead of the Entity of an association?

回眸只為那壹抹淺笑 提交于 2019-12-10 18:09:19

问题


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;
}

and

@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.


回答1:


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;
}

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;
}

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.

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.




回答2:


Sorry, I cant comment so I put it here , I think it should be like that

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

And the other class is :

 @Entity
    public class NodeInnovation {
        @Id
        public long id;
        @OneToMany(mappedBy="origin")
        public List<EdgeInnovation> replacedEdges;
    }

If I'm getting the situation wrong sorry, (Could you draw your classes with the relations so I can get it straight?)




回答3:


Why not use a new construction in JPA and a custom constructor in NodeInnovation? Basically, create a transient property in NodeInnovation for use when you only want the EdgeInnovation id:

@Entity
public class NodeInnovation {
    @Id @GeneratedValue private Long id;
    private Integer type;
    @OneToOne
    private EdgeInnovation replacedEdge;
    @Transient
    private Long replacedEdgeId;
    public NodeInnovation() {}
    public NodeInnovation(Long id, Integer type, Long replacedEdgeId ) {
        this.id = id;
        this.type = type;
        this.replacedEdgeId = replacedEdgeId;
    }
    ...
}

Use it like so:

NodeInnovation n = em.createQuery("select new NodeInnovation(n.id, n.type, n.replacedEdge.id) from NodeInnovation n where n.id = 20", NodeInnovation.class).getSingleResult();

You didn't say how you were selecting NodeInnovation, whether directly or through a join, but either way the trick is the new NodeInnovation in the JPQL or CriteriaBuilder query.



来源:https://stackoverflow.com/questions/36212319/how-do-i-retrieve-only-the-id-instead-of-the-entity-of-an-association

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!