-->
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.  [ 5 posts ] 
Author Message
 Post subject: Question regarding lazy loading
PostPosted: Tue Mar 30, 2010 12:05 pm 
Beginner
Beginner

Joined: Wed Mar 03, 2010 3:35 pm
Posts: 37
I have an enity 'A' which as a collection of enity 'B', the collection is mapped to be lazily loaded which works fine as expected.
Entity 'B' has an one to one relationship with entity 'C' which I have mapped to be lazily loaded by Proxy.But I see that a sql is immediatley executed to fetch enity 'C' whenever the parent enity 'B' is fetched into memory. I guess I am missing some basic concept here, can someone clarify it please.


Top
 Profile  
 
 Post subject: Re: Question regarding lazy loading
PostPosted: Tue Mar 30, 2010 1:41 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Not all one-to-one relations can be proxied. For example if, constrained="true" or if you use property-ref, proxies don't work. See http://docs.jboss.org/hibernate/stable/ ... n-onetoone

If the one-to-one is bi-directional it is always so that only one end can be proxied, the other end is always eagerly fetched.


Top
 Profile  
 
 Post subject: Re: Question regarding lazy loading
PostPosted: Tue Mar 30, 2010 4:24 pm 
Beginner
Beginner

Joined: Wed Mar 03, 2010 3:35 pm
Posts: 37
I have my code below, can somebody point out what is exactly causing the one to one mapped entity 'SubscriberServiceConfig ' not to be lazily fetched.
1)Class 'A'

@Entity
@Table(name = "SERVICE")
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
public class Service implements Serializable {
private static final long serialVersionUID = -5345322513746418441L;

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "EDI_SEQ")
@SequenceGenerator(name = "EDI_SEQ", sequenceName = "EDI_SEQ", allocationSize = 1)
@Column(name = "SERVICE_ID", nullable = false, unique = true)
private Long id;

@OneToMany(mappedBy = "service")
@Cascade( { CascadeType.ALL, CascadeType.DELETE_ORPHAN })
@LazyCollection(LazyCollectionOption.TRUE)
private List<SubscriberServiceReltn> subscrServiceReltns = new ArrayList<SubscriberServiceReltn>();
}

2) Class B
@Entity
@Table(name = "SUBSCRIBER_SERVICE_RELTN")
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
public class SubscriberServiceReltn implements Serializable {

private static final long serialVersionUID = 2843713013040198985L;

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "EDI_SEQ")
@SequenceGenerator(name = "EDI_SEQ", sequenceName = "EDI_SEQ", allocationSize = 1)
@Column(name = "SUBSCRIBER_SERVICE_RELTN_ID", nullable = false, unique = true)
private Long id;

@ManyToOne(targetEntity = com.cerner.edi.dep.subscriber.entity.Subscriber.class, fetch = FetchType.LAZY)
@JoinColumn(name = "SUBSCRIBER_ID", insertable = true, updatable = false, nullable = false)
private Subscriber subscriber;

@ManyToOne(targetEntity = com.cerner.edi.dep.subscriber.entity.Service.class, fetch = FetchType.LAZY)
@JoinColumn(name = "SERVICE_ID", insertable = true, updatable = false, nullable = false)
private Service service;

@OneToOne(mappedBy = "subscriberServiceReltn", fetch = FetchType.LAZY)
@Cascade( { CascadeType.DELETE_ORPHAN, CascadeType.ALL })
private SubscriberServiceConfig subscriberServiceConfig;
}


c) Class 'C'
@Entity
@Table(name = "SUBSCRIBER_SERVICE_CONFIG")
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
public class SubscriberServiceConfig implements Serializable {

private static final long serialVersionUID = 6351762912651912853L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "EDI_SEQ")
@SequenceGenerator(name = "EDI_SEQ", sequenceName = "EDI_SEQ", allocationSize = 1)
@Column(name = "SUBSCRIBER_SERVICE_CONFIG_ID", nullable = false, unique = true)
private Long id;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "SUBSCRIBER_SERVICE_RELTN_ID")
private SubscriberServiceReltn subscriberServiceReltn;
}


Top
 Profile  
 
 Post subject: Re: Question regarding lazy loading
PostPosted: Tue Mar 30, 2010 5:01 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Code:
@OneToOne(mappedBy = "subscriberServiceReltn", fetch = FetchType.LAZY)
@Cascade( { CascadeType.DELETE_ORPHAN, CascadeType.ALL })
private SubscriberServiceConfig subscriberServiceConfig;


The presence of the mappedBy attribute is what is causing this. Even with proxies Hibernate needs know if subscriberServiceConfig should be null or not. This information is only found in the SUBSCRIBER_SERVICE_CONFIG table. Eg. is there is a value in the SUBSCRIBER_SERVICE_RELTN_ID column pointing back to the primary key in SUBSCRIBER_SERVICE_RELTN table or not? So, since Hibernate needs to query the table anyway the entity is loaded eagerly.

In the other direction, the lazy fetching works since Hibernate knows if there is an associated entity or not just by looking at the SUBSCRIBER_SERVICE_RELTN_ID column that is already part of the first query.


Top
 Profile  
 
 Post subject: Re: Question regarding lazy loading
PostPosted: Tue Mar 30, 2010 5:24 pm 
Beginner
Beginner

Joined: Wed Mar 03, 2010 3:35 pm
Posts: 37
Now I understand .Thanks for the reply.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.