-->
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: Why hibernate fetches lazy mapped entities?
PostPosted: Fri Jul 02, 2010 9:56 am 
Beginner
Beginner

Joined: Wed Mar 03, 2010 4:06 am
Posts: 21
Hi,

I have a m:n relationship between customers and accounts: This relationship is resolved in another table customer_accounts. The mapping looks like this:

Code:
@Entity
@Table(name = "customer")

public class Customer implements java.io.Serializable {

   /** The Constant serialVersionUID. */
   private static final long serialVersionUID = -6910750598751114269L;

   /** The person nr. */
   private String customerId;

   /** The kreditkarten. */
   private Set<CustomerAccount> accounts= new HashSet<CustomerAccount>(0);

   /**
    * Instantiates a new Customer .
    */
   public Customer () {
   }

   public void setAccount(Set<CustomerAccount> accounts) {
      this.accounts = accounts;
   }

   @OneToMany(mappedBy = "customer")
   public Set<CustomerAccount> getAccounts() {
      return accounts;
   }

}

/**
* KontoKunde generated by hbm2java.
*/
@Entity
@Table(name = "customer_account")
public class CustomerAccount implements java.io.Serializable {
   
   @Embeddable
   public static class Id implements java.io.Serializable {

      /** The Constant serialVersionUID. */
      private static final long serialVersionUID = -4670709908869684158L;
      
      /** The person nr. */
      private String customerNo;
      
      /** The konto nr. */
      private String accountNo;

      /**
       * Instantiates a new konto kunde id.
       */
      public Id() {
      }

      /** Getter and Setter for Id */
   }

   /** The Constant serialVersionUID. */
   private static final long serialVersionUID = 5462194076796527642L;

   /** The id. */
   private Id id = new Id();

   /** The kunde. */
   private Customer customer;

   /** The konto. */
   private Account account;

   /**
    * Instantiates a new konto kunde.
    */
   public CustomerAccount() {
   }

   
   /*
    * Instantiates a new konto kunde.
    *
    * @param id
    *            the id
    * @param kontoart
    *            the kontoart
    */
   
   /**
    * Erzeugt neue Kunden-Konto Verknuepfung.
    *
    * @param kunde Kunde
    * @param konto Konto
    * @param kontoart Art des Kontos (E Einzel, G Gemeinschaft)
    */
   public CustomerAccount(Customer cust, Account acc) {
      this.id.customerNo = customer.getCustomerNr();
      this.id.accountNo = account.getAccountNo();
            
      customer = cust;
                account = acc;
   }

   /**
    * Gets the id.
    *
    * @return the id
    */
   @EmbeddedId
   @AttributeOverrides( {
         @AttributeOverride(name = "customerNo", column = @Column(name = "customer_no", nullable = false)),
         @AttributeOverride(name = "accountNo", column = @Column(name = "account_no", nullable = false)) })
   public Id getId() {
      return this.id;
   }

   /**
    * Sets the id.
    *
    * @param id
    *            the new id
    */
   public void setId(Id id) {
      this.id = id;
   }

   /**
    * Gets the kunde.
    *
    * @return the kunde
    */
   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "customer_no", nullable = false, insertable = false, updatable = false)
   @org.hibernate.validator.NotNull
   public Kunde getCustomer() {
      return this.customer;
   }

   /**
    * Sets the kunde.
    *
    * @param kunde
    *            the new kunde
    */
   public void setCustomer(Customer cust) {
      this.customer = cust;
      this.id.customerNo = cust.getCustomerNo();
   }

   /**
    * Gets the konto.
    *
    * @return the konto
    */
   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "account_no", nullable = false, insertable = false, updatable = false)
   @org.hibernate.annotations.ForeignKey(name = "fk_kunde_konto_konto_nr")
   public Konto getAccount() {
      return this.account;
   }

   /**
    * Sets the konto.
    *
    * @param konto
    *            the new konto
    */
   public void setAccount(Account acc) {
      this.account= acc;
      this.id.accountNo = acc.getAccountNo();
   }
}

/**
* Konto.
*/
@Entity
@Table(name = "account")
public class Account implements java.io.Serializable {

   /** The Constant serialVersionUID. */
   private static final long serialVersionUID = 3825508146221476748L;

   /** The konto nr. */
   private String accountNo;

   /** Kunden. */
   private Set<CustomerAccount> customer = new HashSet<CustomerAccount>(0);

   /**
    * Instantiates a new konto.
    */
   public Konto() {
   }

   /**
    * Gets the konto nr.
    *
    * @return the konto nr
    */
   @Id
   @Column(name = "account_no", unique = true, nullable = false, length = 10)
   @org.hibernate.validator.NotNull
   @org.hibernate.validator.Pattern(regex = "[0-9]{6,10}+")
   public String getAccountNo() {
      return this.accountNo;
   }

   /**
    * Sets the konto nr.
    *
    * @param kontoNr
    *            the new konto nr
    */
   public void setAccountNo(String accountNo) {
      this.accountNo = accountNo;
   }

   public void setCustomer(Set<CustomerAccount> cust) {
      customer = cust;
   }

   @OneToMany(mappedBy = "account")
   public Set<CustomerAccount> getCustomer() {
      return customer;
   }
}


If I invoke the method findById() of my customerDAO

Code:
   public Customer findById(String customerNo) {
      return em.find(Customer, customerNo);
   }


hibernate fires these two Select statements:

Code:
/* load de.hd.persistence.Customer */ select
        customer0_.customer_no as customer1_595_0_,
        ...
    from
        customer customer0_
    where
        customer0_.customer_nr=?
15:44:21,234 INFO  [STDOUT] Hibernate:
    /* load one-to-many de.hd.persistence.Customer.accounts */ select
        accounts0_.customer_no as customer2_1_,
        accounts0_.account_no as account1_1_,
        accounts0_.account_no as account1_607_0_,
        accounts0_.customer_no as customer2_607_0_
    from
        customer_account account0_
    where
        account0_.customer_no=?


The problem is that I can't understand why hibernate tries to fetch the customer accounts although the mapping defines FetchType.LAZY. May anyone help me out? Thanks a lot.

Jonny


Top
 Profile  
 
 Post subject: Re: Why hibernate fetches lazy mapped entities?
PostPosted: Sat Jul 03, 2010 2:19 pm 
Newbie

Joined: Mon Jun 21, 2010 1:36 pm
Posts: 14
I feel that in your scenario you can just have two Java classes: Customer and Account which will have many-to-many assocation. I am not sure why CustomerAccount class is created here.


Top
 Profile  
 
 Post subject: Re: Why hibernate fetches lazy mapped entities?
PostPosted: Mon Jul 05, 2010 2:51 am 
Beginner
Beginner

Joined: Wed Mar 03, 2010 4:06 am
Posts: 21
Yeah right, but I need to have objects of the type CustomerAccount. The mapping is optional but should not cause problems i guess...


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.