Skip to main content



      Home
Home » Eclipse Projects » EclipseLink » Custom query to load list in bidirectional onetomany relationship
Custom query to load list in bidirectional onetomany relationship [message #1778092] Sat, 09 December 2017 14:06 Go to next message
Eclipse UserFriend
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;
}
Re: Custom query to load list in bidirectional onetomany relationship [message #1778195 is a reply to message #1778092] Mon, 11 December 2017 13:02 Go to previous message
Eclipse UserFriend
Have you seen https://www.eclipse.org/eclipselink/documentation/2.5/solutions/performance001.htm#CHDEGCHH ?
Previous Topic:L2 cache coordination in Wildfly
Next Topic:Blocked - waiting to lock
Goto Forum:
  


Current Time: Sat Jul 05 03:46:10 EDT 2025

Powered by FUDForum. Page generated in 0.03667 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top