Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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.

like image 251
jørgen k. s. Avatar asked Mar 25 '16 00:03

jørgen k. s.


2 Answers

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.

like image 62
Chris Avatar answered Oct 13 '22 05:10

Chris


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?)

like image 2
SeleM Avatar answered Oct 13 '22 06:10

SeleM