-->
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: too many selects w/1-1 legacy mapping
PostPosted: Mon Mar 12, 2007 10:01 am 
Newbie

Joined: Fri Mar 09, 2007 8:47 pm
Posts: 2
When a select is issued as illustrated, there is the expected JOIN issued, but then there is a select issued for every qts_person row that is a User instance; see generated SQL portion.

I'm stuck with the [some?] reference to the AuthUser object; a wrapper for a legacy table.

Is this a bogus strategy/mapping, or is there something else happening here?

Hibernate version:
3.2.0.cr2 (annotations)

Mapping documents:
(annotations, see code)

Code between sessionFactory.openSession() and session.close():
Here's reproducing code and annotated objects stripped to bare minimum:

Code:
Session s = HibernateUtil.getSession();       
s.beginTransaction();       
Criteria q = s.createCriteria( Person.class ).add( Restrictions.eq( "lastName", "Newman" ) );       
List l = q.list();
System.out.println(l.size());
s.getTransaction().rollback();



annotated classes involved:

Code:
@Entity
@Table( name="qts_person" )
@Inheritance( strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn( name="person_class" )
public class Person  {
    private Long id;   
    private String lastName;

    @Id
    @GeneratedValue( strategy=GenerationType.AUTO, generator="SEQ_GEN" )   
    public Long getId() { return id; } 
    public void setId( Long in ) { id = in; }

    @Column( name="last_name", nullable=false )
    public String getLastName() { return lastName; }
    public void setLastName(String lastName) { this.lastName = (lastName != null)?lastName.trim():null; }
}


Code:
@Entity
@DiscriminatorValue( "User" )
public class User extends Person {

    private AuthUser authUser;

    public User() {}
   
    public User( AuthUser user ) {
        setAuthUser( user );
    }   
   
    @OneToOne
    @Fetch( FetchMode.JOIN)   
    @JoinColumn( name="authuser_auth_userid")       
    public AuthUser getAuthUser() {
        return authUser;
    }

    public void setAuthUser(AuthUser authUser) {
        this.authUser = authUser;
    }
}


Code:
@Entity
@Table( name="auth_users_data" )
public class AuthUser  {
    private Long _id;
   
    public AuthUser() {}

    @Id
    @Column( name="AUTH_USERID" )
    public Long getId() {
        return _id;
    }
   
    public void setId(Long id) {
        _id = id;
    }
}


Full stack trace of any exception that occurs:
not an exception; see generated SQL

Name and version of the database you are using:
Oracle 9i

The generated SQL (show_sql=true):
Code:
Hibernate: select this_.id as id0_3_, this_.create_ts as create3_0_3_, this_.creator_id as creator10_0_3_, this_.updater_id as updater11_0_3_, this_.u
pdate_ts as update4_0_3_, this_.active as active0_3_, this_.first_name as first6_0_3_, this_.last_name as last7_0_3_, this_.middle_name as middle8_0_3
_, this_.email as email0_3_, this_.authuser_auth_userid as authuser12_0_3_, this_.person_class as person1_0_3_, person2_.id as id0_0_, person2_.create
_ts as create3_0_0_, person2_.creator_id as creator10_0_0_, person2_.updater_id as updater11_0_0_, person2_.update_ts as update4_0_0_, person2_.active
as active0_0_, person2_.first_name as first6_0_0_, person2_.last_name as last7_0_0_, person2_.middle_name as middle8_0_0_, person2_.email as email0_0
_, person2_.authuser_auth_userid as authuser12_0_0_, person2_.person_class as person1_0_0_, person3_.id as id0_1_, person3_.create_ts as create3_0_1_,
person3_.creator_id as creator10_0_1_, person3_.updater_id as updater11_0_1_, person3_.update_ts as update4_0_1_, person3_.active as active0_1_, pers
on3_.first_name as first6_0_1_, person3_.last_name as last7_0_1_, person3_.middle_name as middle8_0_1_, person3_.email as email0_1_, person3_.authuser
_auth_userid as authuser12_0_1_, person3_.person_class as person1_0_1_, authuser4_.AUTH_USERID as AUTH1_10_2_ from qts_person this_ inner join qts_per
son person2_ on this_.creator_id=person2_.id left outer join qts_person person3_ on person2_.updater_id=person3_.id left outer join auth_users_data au
thuser4_ on person3_.authuser_auth_userid=authuser4_.AUTH_USERID where this_.last_name=?
Hibernate: select authuser0_.AUTH_USERID as AUTH1_10_0_ from auth_users_data authuser0_ where authuser0_.AUTH_USERID=?
Hibernate: select authuser0_.AUTH_USERID as AUTH1_10_0_ from auth_users_data authuser0_ where authuser0_.AUTH_USERID=?
Hibernate: select authuser0_.AUTH_USERID as AUTH1_10_0_ from auth_users_data authuser0_ where authuser0_.AUTH_USERID=?


Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 13, 2007 5:00 pm 
Newbie

Joined: Fri Mar 09, 2007 8:47 pm
Posts: 2
Ok, turns out that the expected method (annotating the field as per the example code) just doesn't work - just is more hibernate BS I guess.

You've got to explicitly set the fetch mode in the query; as in:

Code:
Criteria q = s.createCriteria( Person.class ).setFetchMode( "authUser", FetchMode.JOIN ).add( Restrictions.eq( "lastName", "Newman" ) );



BTW, thanks "flyboy604" for the indirect answer ...


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.