-
1. Re: Performance Problems Selecting EJB's
fernando_jmt Oct 16, 2008 3:33 PM (in response to toni)What about:
entityManager.createQuery("SELECT m FROM Message m left join fetch m.client left join fetch m.partner").getResulList();
HTH.
-
2. Re: Performance Problems Selecting EJB's
toni Oct 16, 2008 5:43 PM (in response to toni)Thanks for the reply!
The Message ejb actually is a subclass of EmailMessage, which itelsef references stores email addresses like this:
@Entity @Name("emailMessage") public class EmailMessage extends CampaignMessage implements Serializable { @OneToOne(cascade = {CascadeType.ALL}) EmailAddress from; @OneToMany(cascade = {CascadeType.ALL}) @JoinTable(name="emailmessage_to") List<EmailAddress> to = new ArrayList<EmailAddress>(); @OneToMany(cascade = {CascadeType.ALL}) @JoinTable(name="emailmessage_cc") List<EmailAddress> cc = new ArrayList<EmailAddress>(); @OneToMany(cascade = {CascadeType.ALL}) @JoinTable(name="emailmessage_bcc") List<EmailAddress> bcc = new ArrayList<EmailAddress>(); @OneToMany(cascade = {CascadeType.ALL}) @JoinTable(name="emailmessage_replyto") List<EmailAddress> replyTo = new ArrayList<EmailAddress>(); @OneToMany(cascade = {CascadeType.ALL}) List<EmailHeader> emailHeaders = new ArrayList<EmailHeader>();
The EmailAddress ejb
@Entity @Name("emailAddress") public class EmailAddress implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) long id; @Email String emailAddress; String name;
About 95% of the selects fetch EmailAdress ejbs:
Hibernate: select emailaddre0_.id as id703_0_, emailaddre0_.emailAddress as emailAdd2_703_0_, emailaddre0_.name as name703_0_ from EmailAddress emailaddre0_ where emailaddre0_.id=?
So following your advice I tried:
entityManager.createQuery("SELECT cm FROM EmailMessage cm left join fetch cm.to left join fetch cm.cc ") // tried only 2 collections, because more than 2 failed too. Only one works, but that still creates all the select statements.
and got the following exception
"javax.ejb.EJBTransactionRolledbackException: org.hibernate.HibernateException: cannot simultaneously fetch multiple bags"
-
3. Re: Performance Problems Selecting EJB's
fernando_jmt Oct 16, 2008 6:06 PM (in response to toni)My example will work for OneToOne and ManyToOne relationships. Your concrete example uses OneToMany, you can try with INNER JOIN FETCH.
-
4. Re: Performance Problems Selecting EJB's
fernando_jmt Oct 16, 2008 6:15 PM (in response to toni)Although this exception is clear enough: cannot simultaneously fetch multiple bags.
-
5. Re: Performance Problems Selecting EJB's
toni Oct 16, 2008 6:37 PM (in response to toni)Do you see any other way how I structure the query in such a way, so to avoid fetching 5000 or so EmailAddress objects?
-
6. Re: Performance Problems Selecting EJB's
barbacena Oct 16, 2008 7:35 PM (in response to toni)Can you use native query?
-
7. Re: Performance Problems Selecting EJB's
obfuscator Oct 18, 2008 10:09 PM (in response to toni)Just google for the multiple bags problem. You need to specify an index column or use sets instead of lists. Specifying index column is probably the best.