I have a jsf + Primefaces + Hibernate application, when I make a query in the database, hibernate returns incomplete objects. This happens for all classes but I will use the class questions as an example:
public class Questoes implements java.io.Serializable {
private Integer id;
private Conteudo conteudo = new Conteudo();
private Dificuldade dificuldade = new Dificuldade();
private Unidade unidade = new Unidade();
private Usuarios usuarios = new Usuarios();
private String descricao;
private boolean publica;
private boolean compartilhada;
private Set questoesProvas = new HashSet(0);
private Set compartilhamentoQuestoeses = new HashSet(0);
private Set alternativases = new HashSet(0);
public Questoes() {
}
public Questoes(Conteudo conteudo, Dificuldade dificuldade, Unidade unidade, Usuarios usuarios, String descricao, boolean publica, boolean compartilhada) {
this.conteudo = conteudo;
this.dificuldade = dificuldade;
this.unidade = unidade;
this.usuarios = usuarios;
this.descricao = descricao;
this.publica = publica;
this.compartilhada = compartilhada;
}
public Questoes(Conteudo conteudo, Dificuldade dificuldade, Unidade unidade, Usuarios usuarios, String descricao, boolean publica, boolean compartilhada, Set questoesProvas, Set compartilhamentoQuestoeses, Set alternativases) {
this.conteudo = conteudo;
this.dificuldade = dificuldade;
this.unidade = unidade;
this.usuarios = usuarios;
this.descricao = descricao;
this.publica = publica;
this.compartilhada = compartilhada;
this.questoesProvas = questoesProvas;
this.compartilhamentoQuestoeses = compartilhamentoQuestoeses;
this.alternativases = alternativases;
}
//getters and setters
XML:
<hibernate-mapping>
<class name="beans.Questoes" table="questoes" catalog="teach_easy" optimistic-lock="version">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<many-to-one name="conteudo" class="br.com.senacrs.Beans.Conteudo" fetch="select">
<column name="idConteudo" not-null="true" />
</many-to-one>
<many-to-one name="dificuldade" class="br.com.senacrs.Beans.Dificuldade" fetch="select">
<column name="idDificuldade" not-null="true" />
</many-to-one>
<many-to-one name="unidade" class="br.com.senacrs.Beans.Unidade" fetch="select">
<column name="idUnidade" not-null="true" />
</many-to-one>
<many-to-one name="usuarios" class="br.com.senacrs.Beans.Usuarios" fetch="select">
<column name="idAutor" not-null="true" />
</many-to-one>
<property name="descricao" type="string">
<column name="descricao" length="65535" not-null="true" />
</property>
<property name="publica" type="boolean">
<column name="publica" not-null="true" />
</property>
<property name="compartilhada" type="boolean">
<column name="compartilhada" not-null="true" />
</property>
<set name="questoesProvas" table="questoes_prova" inverse="true" lazy="true" fetch="select">
<key>
<column name="idQuestao" not-null="true" />
</key>
<one-to-many class="br.com.senacrs.Beans.QuestoesProva" />
</set>
<set name="compartilhamentoQuestoeses" table="compartilhamento_questoes" inverse="true" lazy="true" fetch="select">
<key>
<column name="idQuestao" not-null="true" />
</key>
<one-to-many class="br.com.senacrs.Beans.CompartilhamentoQuestoes" />
</set>
<set name="alternativases" table="alternativas" inverse="true" lazy="true" fetch="select">
<key>
<column name="idQuestao" not-null="true" />
</key>
<one-to-many class="br.com.senacrs.Beans.Alternativas" />
</set>
</class>
ManageBean:
@SessionScoped
@ManagedBean
public class ManageQuestoes {
private Questoes q = new Questoes();
private QuestoesDao dao = new QuestoesDao();
public List listaQuestoes() {
List<Questoes> listaBanco = dao.selectAll(q);
return listaBanco;
}
DAO:
public class QuestoesDao{
private List<Questoes> list;
private Session s;
private Transaction trans;
public List selectAll(Object o) {
s = HibernateUtil.getSessionFactory().openSession();
Criteria criteria = s.createCriteria(o.getClass());
list = criteria.list();
rotine(s);
return this.list;
}
No error is displayed, only the foreign keys are not populated even with the id of the same. the id, description, public, and shared columns are returned perfectly.
Note: this is not working with either Criteria or SQL query, when I run:
List list = s.createQuery("from Questoes");
The problem persisted.