Hibernate brings the incomplete object (without foreign keys)

Asked

Viewed 375 times

0

I have a jsf+Primefaces+Hibernate application, when I go to make a query in the database Hibernate returns incomplete objects. This happens for all classes but I will use the questions class 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 shown, only foreign keys do not come populated even with their id. the id, Description, publish and shared columns are returned perfectly.

Note: is not working with either Criteria or sql query, when running:

List list = s.createQuery("from Questoes");

the problem persisted.

1 answer

1


As stated in the mapping, its Collections linked to the Questao object are "Lazy", check here in the mapping:

<set name="compartilhamentoQuestoeses" table="compartilhamento_questoes" inverse="true" lazy="true" fetch="select">

When a Collection is Lazy, it is loaded only using an explicit request in select, or later loaded. One way to load would be to do the following query:

s.createQuery("from Questoes q JOIN FETCH q.questoesProvas questoesProva JOIN FETCH q.compartilhamentoQuestoeses compartilhamento JOIN FETCH q.alternativases alternativas");

You need to JOIN FETCH for each Lazy attribute you want to load.

Another alternative would be to place the mapping attribute lazy="false" but this is highly contraindicated as it would always carry these Scriptures, even if you didn’t need them. More information on the subject can be found here: http://www.devmedia.com.br/lazy-e-eager-loading-com-hibernate/29554

Browser other questions tagged

You are not signed in. Login or sign up in order to post.