Select, select, select... eta Hibernate

Por que o Hibernate faz tanto select assim a cada requisição?

Hibernate: select this_.id as id3_0_, this_.a_title as a2_3_0_, this_.a_text as a3_3_0_, this_.date_register as date4_3_0_, this_.person_id as person5_3_0_ from article this_ where this_.id=? limit ? Hibernate: select this_.id as id3_0_, this_.a_title as a2_3_0_, this_.a_text as a3_3_0_, this_.date_register as date4_3_0_, this_.person_id as person5_3_0_ from article this_ order by this_.date_register desc limit ? Hibernate: select this_.id as id3_0_, this_.a_title as a2_3_0_, this_.a_text as a3_3_0_, this_.date_register as date4_3_0_, this_.person_id as person5_3_0_ from article this_ order by this_.date_register desc limit ? Hibernate: select this_.id as id5_0_, this_.n_title as n2_5_0_, this_.n_text as n3_5_0_, this_.date_register as date4_5_0_, this_.person_id as person5_5_0_ from notice this_ order by this_.date_register desc limit ? Hibernate: select this_.id as id3_0_, this_.a_title as a2_3_0_, this_.a_text as a3_3_0_, this_.date_register as date4_3_0_, this_.person_id as person5_3_0_ from article this_ where this_.id=? limit ? Hibernate: select this_.id as id3_0_, this_.a_title as a2_3_0_, this_.a_text as a3_3_0_, this_.date_register as date4_3_0_, this_.person_id as person5_3_0_ from article this_ order by this_.date_register desc limit ? Hibernate: select this_.id as id3_0_, this_.a_title as a2_3_0_, this_.a_text as a3_3_0_, this_.date_register as date4_3_0_, this_.person_id as person5_3_0_ from article this_ order by this_.date_register desc limit ? Hibernate: select this_.id as id5_0_, this_.n_title as n2_5_0_, this_.n_text as n3_5_0_, this_.date_register as date4_5_0_, this_.person_id as person5_5_0_ from notice this_ order by this_.date_register desc limit ? Hibernate: select this_.id as id5_0_, this_.n_title as n2_5_0_, this_.n_text as n3_5_0_, this_.date_register as date4_5_0_, this_.person_id as person5_5_0_ from notice this_ where this_.id=? limit ? Hibernate: select comments0_.notice_id as notice6_1_, comments0_.id as id1_, comments0_.id as id6_0_, comments0_.c_title as c2_6_0_, comments0_.c_text as c3_6_0_, comments0_.date_register as date4_6_0_, comments0_.person_id as person5_6_0_, comments0_.notice_id as notice6_6_0_ from t_comment_notice comments0_ where comments0_.notice_id=? order by comments0_.date_register Hibernate: select this_.id as id3_0_, this_.a_title as a2_3_0_, this_.a_text as a3_3_0_, this_.date_register as date4_3_0_, this_.person_id as person5_3_0_ from article this_ order by this_.date_register desc limit ? Hibernate: select this_.id as id3_0_, this_.a_title as a2_3_0_, this_.a_text as a3_3_0_, this_.date_register as date4_3_0_, this_.person_id as person5_3_0_ from article this_ order by this_.date_register desc limit ? Hibernate: select this_.id as id5_0_, this_.n_title as n2_5_0_, this_.n_text as n3_5_0_, this_.date_register as date4_5_0_, this_.person_id as person5_5_0_ from notice this_ order by this_.date_register desc limit ? Hibernate: select this_.id as id3_0_, this_.a_title as a2_3_0_, this_.a_text as a3_3_0_, this_.date_register as date4_3_0_, this_.person_id as person5_3_0_ from article this_ where this_.id=? limit ? Hibernate: select this_.id as id3_0_, this_.a_title as a2_3_0_, this_.a_text as a3_3_0_, this_.date_register as date4_3_0_, this_.person_id as person5_3_0_ from article this_ order by this_.date_register desc limit ? Hibernate: select this_.id as id3_0_, this_.a_title as a2_3_0_, this_.a_text as a3_3_0_, this_.date_register as date4_3_0_, this_.person_id as person5_3_0_ from article this_ order by this_.date_register desc limit ? Hibernate: select this_.id as id5_0_, this_.n_title as n2_5_0_, this_.n_text as n3_5_0_, this_.date_register as date4_5_0_, this_.person_id as person5_5_0_ from notice this_ order by this_.date_register desc limit ? Hibernate: select this_.id as id5_0_, this_.n_title as n2_5_0_, this_.n_text as n3_5_0_, this_.date_register as date4_5_0_, this_.person_id as person5_5_0_ from notice this_ where this_.id=? limit ? Hibernate: select comments0_.notice_id as notice6_1_, comments0_.id as id1_, comments0_.id as id6_0_, comments0_.c_title as c2_6_0_, comments0_.c_text as c3_6_0_, comments0_.date_register as date4_6_0_, comments0_.person_id as person5_6_0_, comments0_.notice_id as notice6_6_0_ from t_comment_notice comments0_ where comments0_.notice_id=? order by comments0_.date_register Hibernate: select this_.id as id3_0_, this_.a_title as a2_3_0_, this_.a_text as a3_3_0_, this_.date_register as date4_3_0_, this_.person_id as person5_3_0_ from article this_ order by this_.date_register desc limit ? Hibernate: select this_.id as id3_0_, this_.a_title as a2_3_0_, this_.a_text as a3_3_0_, this_.date_register as date4_3_0_, this_.person_id as person5_3_0_ from article this_ order by this_.date_register desc limit ? Hibernate: select this_.id as id5_0_, this_.n_title as n2_5_0_, this_.n_text as n3_5_0_, this_.date_register as date4_5_0_, this_.person_id as person5_5_0_ from notice this_ order by this_.date_register desc limit ? Hibernate: select this_.id as id8_0_, this_.f_title as f2_8_0_, this_.f_text as f3_8_0_, this_.date_register as date4_8_0_, this_.person_id as person5_8_0_ from t_forum this_ Hibernate: select this_.id as id8_0_, this_.f_title as f2_8_0_, this_.f_text as f3_8_0_, this_.date_register as date4_8_0_, this_.person_id as person5_8_0_ from t_forum this_ where this_.id=? limit ? Hibernate: select this_.id as id9_0_, this_.t_title as t2_9_0_, this_.t_text as t3_9_0_, this_.date_register as date4_9_0_, this_.person_id as person5_9_0_, this_.t_forum_id as t6_9_0_ from t_topic this_ where this_.t_forum_id=? Hibernate: select this_.id as id9_0_, this_.t_title as t2_9_0_, this_.t_text as t3_9_0_, this_.date_register as date4_9_0_, this_.person_id as person5_9_0_, this_.t_forum_id as t6_9_0_ from t_topic this_ where this_.id=? limit ? Hibernate: select this_.id as id8_0_, this_.f_title as f2_8_0_, this_.f_text as f3_8_0_, this_.date_register as date4_8_0_, this_.person_id as person5_8_0_ from t_forum this_ Hibernate: select this_.id as id8_0_, this_.f_title as f2_8_0_, this_.f_text as f3_8_0_, this_.date_register as date4_8_0_, this_.person_id as person5_8_0_ from t_forum this_ where this_.id=? limit ? Hibernate: select this_.id as id9_0_, this_.t_title as t2_9_0_, this_.t_text as t3_9_0_, this_.date_register as date4_9_0_, this_.person_id as person5_9_0_, this_.t_forum_id as t6_9_0_ from t_topic this_ where this_.t_forum_id=? Hibernate: select this_.id as id8_0_, this_.f_title as f2_8_0_, this_.f_text as f3_8_0_, this_.date_register as date4_8_0_, this_.person_id as person5_8_0_ from t_forum this_ Hibernate: select this_.id as id8_0_, this_.f_title as f2_8_0_, this_.f_text as f3_8_0_, this_.date_register as date4_8_0_, this_.person_id as person5_8_0_ from t_forum this_ where this_.id=? limit ? Hibernate: select this_.id as id9_0_, this_.t_title as t2_9_0_, this_.t_text as t3_9_0_, this_.date_register as date4_9_0_, this_.person_id as person5_9_0_, this_.t_forum_id as t6_9_0_ from t_topic this_ where this_.t_forum_id=? Hibernate: select this_.id as id9_0_, this_.t_title as t2_9_0_, this_.t_text as t3_9_0_, this_.date_register as date4_9_0_, this_.person_id as person5_9_0_, this_.t_forum_id as t6_9_0_ from t_topic this_ where this_.id=? limit ? Hibernate: select this_.id as id8_0_, this_.f_title as f2_8_0_, this_.f_text as f3_8_0_, this_.date_register as date4_8_0_, this_.person_id as person5_8_0_ from t_forum this_ Hibernate: select this_.id as id8_0_, this_.f_title as f2_8_0_, this_.f_text as f3_8_0_, this_.date_register as date4_8_0_, this_.person_id as person5_8_0_ from t_forum this_ where this_.id=? limit ? Hibernate: select this_.id as id9_0_, this_.t_title as t2_9_0_, this_.t_text as t3_9_0_, this_.date_register as date4_9_0_, this_.person_id as person5_9_0_, this_.t_forum_id as t6_9_0_ from t_topic this_ where this_.t_forum_id=?

:shock: :shock: :shock:

Depende como está seu mapeamento !

Ps: Não precisa colocar todo LOG do SQL , fica pesado de mais em ler seu tóipico ! :wink:

[quote=ramilani12]Depende como está seu mapeamento !

Ps: Não precisa colocar todo LOG do SQL , fica pesado de mais em ler seu tóipico ! :wink: [/quote]
Parece que pra cada objeto ele faz um select… imagine que eu tenha 2000 usuários em uma tabela e cada usuário tenha 3 telefones em uma tabela telefones entre outros… ele faria 500000 selects…

Como disse depende do seu mapeamento
Ex:
Se vc excluir um telefone e esse telefone estiver relacionado(obvio) a uma pessoa ele vai na tabela pessoa e deleta o mesmo se aplica a um select e um alter.

Existe um atributo do hibernate lazy=true/false (preguisoço) vc define nos seus hbm´s com isso vc indicará o momento que ele buscará os objetos vc deve ter alguns metodos getTelefones() , getPessoas() algo do tipo bastar setar com true ele se torna lazy-loading

Ex:

<set name="palestras" table="paletra_pessoa" lazy="true">
			<key column="pes_id"/>
      		<many-to-many column="palestra_id" class="entity.Palestra"/>
</set>

Esse exemplo acima de uma coleção “setei” como true o lazy em algum momento buscarei ou seja qndo for solicitado o getPalestras() e o hibernate não fará 4.000 select´s como está acontecendo no seu caso.

Poisé, eu já conheço esse lazy ai… mas e se eu realmente requisitar todas as pessoas e telefones… ele vai fazer mil selects… uso lazy quando não preciso de tudo imediatamente… mas e se eu precisar?

Sugiro que leia a documentação antes de usar o Hibernate.

Leia também sobre como configurar o cache do Hibernate. Faz toda a diferença.