-->
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.  [ 3 posts ] 
Author Message
 Post subject: initialize set with its dependency
PostPosted: Mon Jan 05, 2009 6:39 pm 
Newbie

Joined: Tue Mar 11, 2008 4:49 am
Posts: 12
Hi

I have a question if it is possible to somehow initialize proxy collection by Hibernate.initialize() call and have each element of the collection fetch join its one2one/many2one association?

Let's make myself clear be simple example:

Code:
@Entity
@Table(name = "customer")
public class Customer {
   @Id
   @Column(name = "cus_id", unique = true)
   private Long customerId;

   @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "customer")
   private Set<CustomerAccount> accounts= new HashSet<CustomerAccount>();
}

Code:
@Entity
@Table(name = "customer_account")
public class CustomerAccount{
   @Id
   @Column(name = "cac_id")
   private Long id;

   @ManyToOne(optional = true)
   @JoinColumn(name = "cac_cus_id", referencedColumnName = "cus_id")
   private Customer customer;

   @ManyToOne(optional = true)
   @JoinColumn(name = "cac_ccy_symbol", referencedColumnName = "ccy_symbol")
   private Currency accountCurrency;
}

Code:
@Entity
@Table(name = "currency")
public class Currency{

   @Id
   @Column(name = "ccy_symbol")
   private String symbol;

   @Column(name = "ccy_code", nullable = false)
   private Long code;

   @Column(name = "ccy_precision", nullable = false)
   private int precision;
}


In My DAO i execute:
Code:
//find customer
Customer customerDetails = currentSession.createQuery("from Customer  where ... "
//
Hibernate.initialize(customerDetails.getAccounts());
for (CustomerAccount ca : customerDetails.getAccounts()) {
    Hibernate.initialize(ca.getAccountCurrency());
}


Obviuosly it leads to n+1 select problems.
Is it any way to initialize set of accounts and tell hibernate to eagerly load currency for each of the account.
From SQL pont of view the query would be trivial...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2009 2:23 pm 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
Currency sounds like reference data to me. Why not make it cacheable? This will avoid the database hit entirely, problem gone.

-Chris

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


Top
 Profile  
 
 Post subject: Re: initialize set with its dependency
PostPosted: Tue Aug 04, 2009 10:19 am 
Newbie

Joined: Tue Mar 11, 2008 4:49 am
Posts: 12
It does not matter if currency can be cacheable or not since
the code presented above is only an example to clarify my question.
Still help needed with this issue.


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

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.