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