-->
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: Lazy OneToOne
PostPosted: Sat Jan 23, 2010 10:41 am 
Pro
Pro

Joined: Wed Nov 05, 2003 7:22 pm
Posts: 211
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


Top
 Profile  
 
 Post subject: Re: Lazy OneToOne
PostPosted: Sat Jan 23, 2010 1:48 pm 
Pro
Pro

Joined: Wed Nov 05, 2003 7:22 pm
Posts: 211
Actually scrap that. I'm back at: it's impossible again.
I'm getting a "null id generated for:class Subscription" when trying to insert a User. With the following code.
Sigh.

Code:
User
        public Usor() {
      subscription = new Subscription();
      subscription.setUser(this);
        }

   @OneToOne(optional = false, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
   @PrimaryKeyJoinColumn
   public Subscription getSubscription() {
      return subscription;
   }

Subscription
   @Id
   @GeneratedValue(generator="foreign")
   @GenericGenerator(name="foreign", strategy = "foreign",parameters={@Parameter(name="property",value="user")})
   @Column(name="userId")
   public Long getId() {
      return id;
   }

   @OneToOne(mappedBy="subscription",optional=false)
   @JoinColumn(name="id")
   public Usor getUser() {
      return user;
   }


Top
 Profile  
 
 Post subject: Re: Lazy OneToOne
PostPosted: Sat Jan 23, 2010 5:29 pm 
Pro
Pro

Joined: Wed Nov 05, 2003 7:22 pm
Posts: 211
Went back to this, which works, including lazy. I think a lot of the lazy loading confusion may be caused by the fact that a developer may accidently reference a property which leads to the loading of the lazy property, as was the case with me. And then you get lead down the dark path of the "other" OneToOne options and chaos ensues :-).
Code:
User
   @OneToOne(optional = false, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
   @JoinColumn(name = "FK_SubscriptionId", nullable = false)
   public Subscription getSubscription() {
      return subscription;
   }

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

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


I think that a shared primary key is more elegant, but oh well.

Cheers,

Marc


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.