-->
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: OneToOne Lazy instantiation
PostPosted: Thu Jan 14, 2010 5:39 am 
Pro
Pro

Joined: Wed Nov 05, 2003 7:22 pm
Posts: 211
Hi,

I've read about the challenges in getting a OneToone relationship lazily instantiated and I've tried to implement it properly but have run into a strange result. In the same class on one identically set OneToOne relationship, lazy works. On the other it doesn't. I'm looking for tips where to look for the issue here.

I have two relationships set up in Class User:
Code:
*User*
@Entity(name = "User2")
@Table(name = "User")
@Indexed

   @OneToOne(optional = false, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
   @JoinColumn(name = "FK_SubscriptionID", nullable = false)
   public Subscription getSubscription() {
      return subscription;
   }

   @OneToOne(optional = false, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
   @JoinColumn(name = "FK_VendorRatingId", nullable = false)
   public VendorRating getVendorRating() {
      return vendorRating;
   }
*Subscription
@Entity
@Table(name = "Subscription")

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   public Long getId() {
      return id;
   }

   @OneToOne(mappedBy="subscription")
   public Usor getUser() {
      return user;
   }

*Rating*
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorValue(value = "RAT")
@Table(name="rating")
@Indexed

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "vendorratingId", updatable = false, insertable = false)
   public Long getId() {
      return id;
   }

*Vendor Rating*
@Entity
@DiscriminatorValue(value = "VDR")

   @OneToOne(mappedBy="vendorRating")
   public Usor getUser() {
      return user;
   }



The vendor rating is not retrieved when I retrieve the user. However, for the subscription, two queries are executed each time the user is retrieved to get the subscription. Both vendorRating and Subscription have hashcode/equals functions that reference the user variable. All classes implement Serializable. None of the fields in subscription are referenced in the code.

E.g.
Retrieve user using find by email.
1. user record is retrieved
2. subscription record is retrieved based on subscription id
3. user and subscription record are retrieved based on sunscription id

I've tried using a LazyToOne and a Proxy for Subscription that didn't help.

I'm probably missing something obvious. Any ideas?

Kind regards,

Marc


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.