Initialize a Lazy list in hibernate using a criterion with a parameterized SetAlias

1

I have the following code:

criterio = session.createCriteria(Tecnico.class)

  .add(Restrictions.eq("expediente", usuario))
  .add(Restrictions.eq("password", contrasenia))
  .createAlias("empresa", "empresa", JoinType.LEFT_OUTER_JOIN)
  .setFetchMode("empresa", FetchMode.JOIN)                    
  .createAlias("grupos", "grupos", JoinType.LEFT_OUTER_JOIN, Restrictions.eq("grupos.estatus", true))                        
  .setFetchMode("grupos", FetchMode.JOIN)

When I recover the value of the group list, it has not been initialized and the company value has. On the other hand, if I remove the condition Restrictions.eq("grupos.estatus", true) , yes, initialize the list groups. What can I do to be able to initialize the list without having to remove the restriction in the clause?

Class Tecnico :

@Entity
@Table(name = "CAT_TECNICOS", schema = "SATEC", uniqueConstraints = @UniqueConstraint(columnNames = "EXPEDIENTE"))
public class Tecnico implements java.io.Serializable {

    private static final long serialVersionUID = 325857236592549406L;

    @Id
    @Column(name = "IDTECNICO", unique = true, nullable = false, precision = 8, scale = 0)
    private int idtecnico;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "IDEMPRESA", nullable = false)
    private Empresa empresa;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "IDESTATUS_OCUPACION", nullable = false)
    private EstatusOcupacion estatusOcupacion;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "IDHABILIDAD", nullable = false)
    private Habilidad habilidad;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "IDHORARIO", nullable = false)
    private Horario horario;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "IDPERFIL", nullable = false)
    private Perfil perfil;

    @Column(name = "EXPEDIENTE", unique = true, nullable = false, length = 16)
    private String expediente;

    @Column(name = "PASSWORD", nullable = false, length = 8)
    private String password;

    @Column(name = "NOMBRE", nullable = false, length = 200)
    private String nombre;

    @Column(name = "ACTIVO", nullable = false, length = 1)
    private char activo;

    @Column(name = "AGENDA", nullable = false, precision = 8, scale = 0)
    private int agenda;

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "tecnico")
    private Set<EstadoAsignacion> estadoAsignaciones = new HashSet(0);

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "tecnico")
    private Set<InventarioDispositivo> inventarioDispositivos = new HashSet(0);

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "tecnico")
    private Set<Biometrico> biometricos = new HashSet(0);

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "tecnico")
    private Set<Grupo> grupos = new HashSet(0);

Class Grupo :

@Entity
@Table(name = "TB_GRUPOS", schema = "SATEC", uniqueConstraints = {
    @UniqueConstraint(columnNames = "IDTECNICO"),
    @UniqueConstraint(columnNames = "IDGRUPO")}
)
public class Grupo implements java.io.Serializable {

    private static final long serialVersionUID = 8241096129146860864L;

    @EmbeddedId
    @AttributeOverrides({
        @AttributeOverride(name = "idGrupo", column = @Column(name = "IDGRUPO", unique = true, nullable = false, precision = 22, scale = 0)),
        @AttributeOverride(name = "idtecnico", column = @Column(name = "IDTECNICO", unique = true, nullable = false, precision = 22, scale = 0))})
    private GrupoPk grupoPk;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "IDTECNICO", unique = true, nullable = false, insertable = false, updatable = false)
    private Tecnico tecnico;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "IDGRUPO", unique = true, nullable = false, insertable = false, updatable = false)
    private GrupoMensaje grupoMensaje;

    @Temporal(TemporalType.DATE)
    @Column(name = "FEC_ACTUALIZACION", nullable = false, length = 7)
    private Date fecActualizacion;

    @Column(name = "ESTATUS", nullable = false, length = 1)
    private boolean estatus;
    
asked by Marco Antonio Mota 10.03.2017 в 16:42
source

1 answer

1

Using the method:

Hibernate.initialize(Object obj);

With which to initialize the object you need when you are lazy.

You can also change the type of fetchtype from "LAZY" to "EAGER". I should load it in memory, but it's not the most efficient because I would load it whenever you call an object Tecnico .

As a last option, you could launch a query for each Technician, who will return the collection of their groups.

Select t.grupos from Tecnico t where t.id =?1;

Sources:

Hibernate: best practice to pull all lazy collections

    
answered by 28.05.2017 / 19:30
source