当我尝试从数据库中检索一些实体时,我遇到了一个奇怪的问题。实体所在的表只有 4 行。当我尝试选择所有行时,我得到一个列表,其中第一个和最后一个元素加载正确,但是,第二个和第三个的所有属性都为空。这是我的调试控制台的打印:
实体很简单,如下所示:
@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;
}
如果你想知道我是如何检索实体的,这里是代码:
@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();
}