Reputation: 145
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.
Upvotes: 7
Views: 12677
Reputation: 15
I am aware I am quite late but some people might look for an answer to the same question - in your JPA repository you could do something like this:
@Query("SELECT new java.lang.Integer(model.id) FROM #{#entityName} model WHERE model.relationModeFieldlName.id IN :relationModelIds")
List<Integer> findIdByRelationModelIdIn(@Param("relationModelIds") List<Long> relationModelIds);
Upvotes: 0
Reputation: 21145
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.
Upvotes: 8
Reputation: 11551
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.
Upvotes: 0
Reputation: 9638
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?)
Upvotes: 2