Hi,
I've read a lot of blog posts and documentation on getting OneToOne to work in a lazy fashion and none seemed to work for me.
Some posts said: it's not possible, period. Some say: I did it (on reproduction it doesn't work). And even the Hibernate documentation explains how to do it:
https://www.hibernate.org/162.html. Still no joy.
It does in fact work, but you have to be careful not be stupid
So, I have say a class User and Subscription.
Code:
@Entity
@Table
@Indexed
public class Usor implements Serializable, UserDetails {
User:
@OneToOne(optional = false, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@PrimaryKeyJoinColumn
public Subscription getSubscription() {
return subscription;
}
Subscription:
@Entity
@Table
public class Subscription implements Serializable{
@Id
@GeneratedValue(generator="foreign")
@GenericGenerator(name="foreign", strategy = "foreign",parameters={@Parameter(name="property",value="user")})
@Column(name="subscriptionId", nullable=true, insertable=true, updatable=false)
public Long getId() {
return id;
}
@OneToOne(mappedBy="subscription")
public Usor getUser() {
return user;
}
Whenever a user is retrieved, I kept retrieving the subscription. As it turns out, the reason was that whenever a user was created, I used the User constructor to set some properties on the subscription.
Another example of a onetoone entity refused to be lazy was because I did something like
Code:
public void setSubscription(Subscription sub){
this.subscription = sub;
sub.setUser(this);
}
Just wanted to share this.
Kind regards,
Marc