brevleq
brevleq

Reputation: 2141

Why I'm getting Javassist objects in the middle of a result set?

I'm having an strange problem when I try to retrieve some entities from the database. The table where the entities lives just have 4 rows. When I try select all rows I get a list where the first and the last elements are loaded correct, however, the second and the third has all properties as null. Here is a print of my debug console:

javassist problem debug

The entity is simple, as you can see below:

@Entity
@Table(name = "Empresa")
public class Empresa implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID_EMPRESA")
    private Integer idEmpresa;
    @Basic(optional = false)
    @Column(name = "NOME_EMPRESA")
    @OrderColumn
    private String nomeEmpresa;
    @Column(name = "CNPJ")
    private String cnpj;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "iDEmpresa", fetch = FetchType.LAZY)
    private List<Cadastro> cadastroList;

}

If you want know how I am retrieving the entities, here is the code:

@Override
public List<T> recuperarTodos() {
    Query query = entityManager.createQuery(criarQueryRecuperarTodos());
    limitarQuantidadeDeRegistros(query);
    return query.getResultList();
}

private String criarQueryRecuperarTodos() {
    StringBuilder builder = new StringBuilder("SELECT e FROM ");
    builder.append(classe.getSimpleName());
    builder.append(" e");
    builder.append(criarParametrosOrdenacao());
    return builder.toString();
}

Upvotes: 3

Views: 2185

Answers (1)

Marcin Łoś
Marcin Łoś

Reputation: 3246

This is perfectly legal and expected situation. Hibernate uses dynamically generated proxies (hence javaassist objects, in the past hibernate used cglib as well) as placeholders for not fully fetched entities to allow lazy fetching. Because of this, generally speaking, you should not attempt to access attribute values directly. Using getters instead allows hibernate to issue an appropriate DB query and fill the entity. This can be a problem in some situations - for example, if the values are first requested outside the Hibernate session.

Upvotes: 2

Related Questions