-->
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.  [ 2 posts ] 
Author Message
 Post subject: OneToOne Proxy question in Hibernate 3.2
PostPosted: Wed May 31, 2006 8:31 pm 
Regular
Regular

Joined: Thu Aug 28, 2003 6:30 am
Posts: 58
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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 4:39 am 
Regular
Regular

Joined: Thu Aug 28, 2003 6:30 am
Posts: 58
Also i tried to add optional=false attribute to @OneToOne annotation, but the result was the same.
First select to get user, then select to get Portfolio and again getting user.


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