java - Improving Hibernete performance in many-to-many relationship -


i have 2 tables connected many-to-many relationship. database set on server , see big performance problem when i'm trying informations 1 of records if these informations include total count of second table.

first bean:

package dbaccess.beans.newsletter;  import java.sql.timestamp; import java.util.hashset; import java.util.set;  import javax.persistence.entity; import javax.persistence.fetchtype; import javax.persistence.generatedvalue; import javax.persistence.generationtype; import javax.persistence.id; import javax.persistence.joincolumn; import javax.persistence.jointable; import javax.persistence.manytomany; import javax.persistence.sequencegenerator; import javax.persistence.column; import javax.persistence.table;  import dbaccess.beans.registereduser;  @entity @table(name="newsletter_list") public class newsletterlist {      @id     @generatedvalue(strategy = generationtype.sequence, generator = "g1")     @sequencegenerator(name = "g1", sequencename = "newsletter_list_seq", allocationsize = 1, initialvalue= 1)     @column(name = "list_id", unique = true, nullable = false)     private long listid;      @column(name = "list_name", nullable = false, length = 50)     private string listname;      @manytomany(fetch = fetchtype.lazy, cascade = {})     @jointable(name = "newsletterlists_users", joincolumns = {              @joincolumn(name = "list_id", nullable = false) },              inversejoincolumns = { @joincolumn(name = "user_id", nullable = false) })     private set<registereduser> users = new hashset<registereduser>(0);      public long getlistid() {         return listid;     }      public void setlistid(long listid) {         this.listid = listid;     }      public set<registereduser> getusers() {         return users;     }      public void setusers(set<registereduser> users) {         this.users = users;     }  } 

second bean:

package dbaccess.beans;  import java.sql.timestamp; import java.util.hashset; import java.util.set;  import javax.persistence.entity; import javax.persistence.fetchtype; import javax.persistence.generatedvalue; import javax.persistence.generationtype; import javax.persistence.id; import javax.persistence.manytomany; import javax.persistence.sequencegenerator; import javax.persistence.column; import javax.persistence.table;  import dbaccess.beans.newsletter.newsletterlist;  @entity @table(name="user") public class registereduser {      @id     @generatedvalue(strategy = generationtype.sequence, generator = "g1")     @sequencegenerator(name = "g1", sequencename = "user_seq", allocationsize = 1, initialvalue= 1)     @column(name = "user_id", unique = true, nullable = false)     private long usrid;      @column(name = "givenname", length = 20)     private string usrgivenname;      @column(name = "familyname", length = 20)     private string usrfamilyname;      @manytomany(fetch = fetchtype.eager, mappedby = "users", cascade = {})     public set<newsletterlist> newsletterlist = new hashset<newsletterlist>();      public long getusrid() {         return usrid;     }      public void setusrid(long usrid) {         this.usrid = usrid;     }      public string getusrgivenname() {         return usrgivenname;     }      public void setusrgivenname(string usrgivenname) {         this.usrgivenname = usrgivenname;     }      public string getusrfamilyname() {         return usrfamilyname;     }      public void setusrfamilyname(string usrfamilyname) {         this.usrfamilyname = usrfamilyname;     }      public set<newsletterlist> getnewsletterlist() {         return newsletterlist;     }      public void setnewsletterlist(set<newsletterlist> newsletterlist) {         this.newsletterlist = newsletterlist;     }      @override     public string tostring() {         return "registereduser[usrid=" + usrid + ", usrgivenname=" + usrgivenname + ", usrfamilyname=" + usrfamilyname + "]";     }  } 

and problem when try execute piece of code:

session = dbservice.getsessionfactory().opensession(); criteria c = session.createcriteria(newsletterlist.class); c.add(restrictions.eq("listid", listid)); list<newsletterlist> newsletterlist = (list<newsletterlist>) c.list();  //below expensive newsletterlist.get(0).getusers().size() 

is there way improve performance? in advance.

ps when have approx. 70 users in 1 list, request above code takes approx 5-6 seconds!

newsletterlist.get(0).getusers().size() makes hibernate load users registered newsletter, number of registered users.

use ad hoc hql query count number of registered users:

select count(user.usrid) registereduser user inner join user.newsletterlist newsletter newsletter.listid = :listid 

note 5-6 seconds execute above code way much, though. need check if there index placed on join columns of join table.

also note use session.get(newsletter.class, listid) list id.

and finally, easier , more readable if ids named id.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -