-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: @OneToMany causing multiple select statements
PostPosted: Mon Jan 12, 2015 9:05 am 
Newbie

Joined: Wed Nov 28, 2012 7:37 am
Posts: 4
Hi,

I am working on a Hibernate issue, which involves 2 separate Entity beans defined separately in their own classes:

- Store
- StoreServer

Note that a Store will have more than one StoreServer - hence the use of the @OneToMany annotation. Please see the code snippets as follows:

Store:
Code:
@Entity
@Table(name="Store")
public class Store implements Serializable {
   /**
    * Serializable class - generated UID
    */
   private static final long serialVersionUID = 5644190852867691168L;
   
   @Id
   @Column(name="STORE_NO", nullable=false)
   private int storeNumber;
   
   @Column(name="STORE_NAME", nullable=false)
   private String storeName;
   
   @Column(name="STORE_PHONE", nullable=false)
   private String storePhone;

        //other Store fields...

   @OneToMany(fetch = FetchType.EAGER)
        @JoinColumn(name="STORE_NO", insertable=false, updatable=false)
   private List<StoreServer> storeServers = new ArrayList<StoreServer>();

        //getters and setters


StoreServer:
Code:
@Entity
@Table(name="Store_Server")
public class StoreServer implements Serializable {
   /**
    * Serializable class - generated UID
    */
   private static final long serialVersionUID = -5410564578856243437L;
   
   @Id
   private StoreServerPK storeServerPK;

   @Column(name="IP_ADDRESS", nullable=true)
   private String ipAddress;

        //other StoreServer fields...getters and setters


Since StoreServer has a composite Primary Key, here is StoreServerPK:
Code:
@Embeddable
public class StoreServerPK implements Serializable {
   /**
    * Serializable class - generated UID
    */
   private static final long serialVersionUID = -1401889029390423604L;
   
   @Column(name="STORE_NO", nullable=false)
   protected int storeNumber;
   
   @Column(name="SERVER_NO", nullable=false)
   protected String serverNumber;

        //getters and setters


At present, I am getting the correct results, but the performance is unacceptably SLOW. I have switched on logging in Hibernate and I can see that a separate SELECT query is being run for each Store Entity in order to obtain the associated StoreServer records.

Currently, in the logs, I see a single SELECT statement to obtain the Store records (more than 200 results returned). Then for each store, a new SELECT statement to get the StoreServer records. My question is...Why is Hibernate not doing a join (running one query)?

Please could I get some help on how to tell Hibernate to run a single query, using a JOIN?

Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.