Custom query to load list in bidirectional onetomany relationship [message #1778092] |
Sat, 09 December 2017 14:06  |
Eclipse User |
|
|
|
I have a bidirectional onetomany relationship in this entities: Actividad and TagVinculado
TagVinculado entity has ManyToOne relationship with Tag
Tag entity has ManyToOne relationship with Tipo
When I access to tagsVinculados list from actividad entity, eclipselink create this queries:
1 query for tag_vinculados and for each tagVicnulado in list generetes 1 query for Tag and 1 query for Tag.Tipo
SELECT * FROM tags_vinculados WHERE (idActividad = ?)
SELECT * FROM tags WHERE (id = ?)
SELECT * FROM tipos WHERE (id = ?)
...
SELECT * FROM tags WHERE (id = ?)
SELECT * FROM tipos WHERE (id = ?)
Exist any way to custom the queries to load this list?
I would load this list with single query with 2 joins:
select tagVinculado
from TagVinculado tagVinc
inner join fetch tagVinc.tag tag
inner join fetch tagVinc.tag.tipo tipoTag
where tagVinc.actividad.id = ?
@Entity
@Table(name = "actividades")
public class Actividad Serializable {
@Column(name = "nombre")
private String nombre;
@OneToMany(mappedBy = "actividad", fetch=FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
private List<TagVinculado> tagsVinculados;
}
@Entity
@Table(name = "tags_vinculados")
public class TagVinculado implements Serializable {
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "idTag")
private Tag tag;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "idActividad")
private Actividad actividad;
}
@Entity
@Table(name = "tags")
public class Tag Serializable {
@Column(name = "nombre")
private String nombre;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "idTipo")
private Tipo tipo;
}
@Entity
@Table(name = "tipos")
public class Tipo Serializable {
@Column(name = "nombre")
private String nombre;
}
|
|
|
|
Powered by
FUDForum. Page generated in 0.03667 seconds