Hibernate version:3.2R2, Annotations 3.2.0cr1
Name and version of the database you are using:Postgres8
Hi everybody.
I've got a question for lazy loading and proxy classes.
I have 2 objects with one-one relation:
Code:
@BatchSize(size = 5)
@Entity
@Table(name = "my_user")
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Proxy(lazy = true, proxyClass = com.darkit.core.dao.hibernate.persistent.User.class)
@javax.persistence.SequenceGenerator(
name = "SEQ_STORE",
sequenceName = "user_sequence"
)
public class User extends AbstractIDComparable implements IUser {
....
@OneToOne(targetEntity = com.darkit.core.dao.hibernate.persistent.Portfolio.class,
cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "portfolio_id", nullable = true)
public IPortfolio getPortfolio() {
return portfolio;
}
public void setPortfolio(final IPortfolio portfolio) {
this.portfolio = portfolio;
}
...
And Portfolio Object:
Code:
@BatchSize(size = 5)
@Entity
@Table(name = "portfolio")
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Proxy(lazy = true, proxyClass = com.darkit.core.dao.hibernate.persistent.Portfolio.class)
@javax.persistence.SequenceGenerator(
name = "SEQ_STORE",
sequenceName = "portfolio_sequence"
)
public class Portfolio extends AbstractIDComparable implements IPortfolio {
...
@OneToOne(targetEntity = com.darkit.core.dao.hibernate.persistent.User.class,
mappedBy = "portfolio", fetch = FetchType.LAZY)
public IUser getUser() {
return user;
}
public void setUser(final IUser user) {
this.user = user;
}
...
}
I retrieve user by its Id
Code:
hibernateTemplate.get(User.class, id);
Hibernate generates such query:
Code:
select user0_.id as id0_0_, user0_.portfolio_id as portfolio11_0_0_, user0_.login as login0_0_ from stockwit_user user0_ where user0_.id=?
But when i invoke any getter methond in Portfolio, Hibernate generates not one sql query to get Portfolio but 2 queries:
Code:
select portfolio0_.id as id5_0_, portfolio0_.cash as cash5_0_, portfolio0_.portfolio_value as portfolio3_5_0_ from portfolio portfolio0_ where portfolio0_.id=?
select user0_.id as id0_0_, user0_.portfolio_id as portfolio11_0_0_, user0_.login as login0_0_ from stockwit_user user0_ where user0_.portfolio_id=?
Does any one knows why hibernate does it ?
I thought it shouldn't try to get User when it selects Portfolio.
Thank's