hibernate too many sql -


i have test1 class , test2 class

they 1 one , fetch type lazy.

@entity @table(name = "test1") public class test1 {     @id     @generatedvalue(strategy = generationtype.identity)     @column     private int id;      @column(name="name")     private string name;      @onetoone(cascade = cascadetype.all,fetch = fetchtype.eager)     @joincolumn(name = "test2_id")     private test2 test2;      ... }  @entity @table(name = "test2") public class test2 {     @id     @generatedvalue(strategy = generationtype.identity)     @column     private int id;      @column(name="name")     private string name;      @onetoone(mappedby = "test2")      ...  } 

i want records database :

    criteria dc2 =     sessionfactory. getcurrentsession().createcriteria(test1.class,"mr");     list<test1> reports2= dc2.list();     for(test1 test:reports2){         test.gettest2().getname();     } 

but console show sql is:

   hibernate: select this_.id id18_0_, this_.name name18_0_, this_.test2_id test3_18_0_ test1 this_    hibernate: select test2x0_.id id19_1_, test2x0_.name name19_1_, test1x1_.id id18_0_, test1x1_.name name18_0_, test1x1_.test2_id test3_18_0_ test2 test2x0_ left outer join test1 test1x1_ on test2x0_.id=test1x1_.test2_id test2x0_.id=?    hibernate: select test1x0_.id id18_0_, test1x0_.name name18_0_, test1x0_.test2_id  test3_18_0_ test1 test1x0_ test1x0_.test2_id=? 

because fetch lazy ,i think sql right,

and change fetch type fetchtype.earger

hibernate: select this_.id id18_1_, this_.name name18_1_, this_.test2_id test3_18_1_, test2x2_.id id19_0_, test2x2_.name name19_0_ test1 this_ left outer join test2 test2x2_ on this_.test2_id=test2x2_.id hibernate: select test1x0_.id id18_1_, test1x0_.name name18_1_, test1x0_.test2_id test3_18_1_, test2x1_.id id19_0_, test2x1_.name name19_0_ test1 test1x0_ left outer join test2 test2x1_ on test1x0_.test2_id=test2x1_.id test1x0_.test2_id=? 

i not understand second sql,it waste of time

if have 10000 records , circle 10000 times.

how can resole ? records in times?

you may use setfetchmode join...for example

 criteria dc2 = sessionfactory. getcurrentsession().createcriteria(test1.class,"mr");  dc2.setfetchmode("test2", fetchmode.join)     list<test1> reports2= dc2.list();     for(test1 test:reports2){         test.gettest2().getname();     } 

in way hibernate should fill test2 object 1 query; can't performances since in scenario more data loaded db

i hope helps

angelo


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -