-->
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.  [ 7 posts ] 
Author Message
 Post subject: Why the subclass also is fetched when querying on its super
PostPosted: Tue Jul 21, 2009 2:05 am 
Regular
Regular

Joined: Tue Feb 17, 2009 3:36 pm
Posts: 78
I have a pair superclass and subclass defined as the followings:
Code:
@MappedSuperclass
@Entity
@Table(name = "event")
public class EventInfo implements Serializable {
   protected Integer id;
   protected GroupInfo group;
//...
}

@Entity
@Table(name = "event")
public class Event extends EventInfo {
//...
}

When I fetch the superclass as
Code:
"from EventInfo e where e.group.id=:gid ..."

A query on its subclass also is generated in addition of a query on the superclass. As a result, I have two same items in the returned list. Although this problem is easy to fix with a query like
Code:
"select new EventInfo(e.id, ...) from Event e ..."

on the subclass, I would like to know the causes of this problem.


Top
 Profile  
 
 Post subject: Re: Why the subclass also is fetched when querying on its super
PostPosted: Tue Jul 21, 2009 4:44 am 
Regular
Regular

Joined: Thu Apr 14, 2005 10:39 am
Posts: 115
Hi, without the GroupInfo class, I can't tell you. But keep in mind that one2one and Many2one relations are fetched eager by default. If you don't want this behaviour, just force lazy. (E.g. with
Code:
@Basic(fetch = FetchType.LAZY)
)

You can eliminate multiple results with:
Code:
q.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);



Greetings Michael


Top
 Profile  
 
 Post subject: Re: Why the subclass also is fetched when querying on its super
PostPosted: Tue Jul 21, 2009 12:52 pm 
Regular
Regular

Joined: Tue Feb 17, 2009 3:36 pm
Posts: 78
Urmech wrote:
Hi, without the GroupInfo class, I can't tell you. But keep in mind that one2one and Many2one relations are fetched eager by default. If you don't want this behaviour, just force lazy. (E.g. with
Code:
@Basic(fetch = FetchType.LAZY)
)

You can eliminate multiple results with:
Code:
q.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);



Greetings Michael


Thanks for your input. I, however, am unsure it is related with the fetch type. The multiple queries is not on the its child class at all. It is about class hierarchy, but not parent-child relationship.

Here is the two queries generated by the single HQL:
Code:
Hibernate:
    select
        eventinfo0_.id as id26_,
        eventinfo0_.ending as ending26_,
        eventinfo0_.group_fk as group13_26_,
        eventinfo0_.name as name26_,
        eventinfo0_.starting as starting26_
    from
        se_event eventinfo0_
    where
        eventinfo0_.group_fk=?
        and eventinfo0_.starting>=?
    order by
        eventinfo0_.starting desc
Hibernate:
    select
        event0_.id as id26_,
        event0_.ending as ending26_,
        event0_.group_fk as group13_26_,
        event0_.name as name26_,
        event0_.starting as starting26_,
        event0_.access as access26_,
        event0_.description as descript8_26_,
        event0_.fee as fee26_,
        event0_.limitation as limitation26_,
        event0_.location as location26_,
        event0_.ORGANIZER_FK as ORGANIZER14_26_,
        event0_.status as status26_
    from
        se_event event0_
    where
        event0_.group_fk=?
        and event0_.starting>=?
    order by
        event0_.starting desc


Top
 Profile  
 
 Post subject: Re: Why the subclass also is fetched when querying on its super
PostPosted: Fri Jul 24, 2009 6:07 pm 
Regular
Regular

Joined: Thu Apr 14, 2005 10:39 am
Posts: 115
Hi,
Quote:
@MappedSuperclass
@Entity


what do you want to do with this?
Have you looked at your db- scheme?

Normally you use @Entity or @MappedSuperclass not both.
http://java.sun.com/javaee/5/docs/api/j ... class.html

Quote:
Designates a class whose mapping information is applied to the entities that inherit from it. A mapped superclass has no separate table defined for it.


So it is no surprise, that the subclass is fetched, the superclass has no table! I assume @Entity is ignored, if you don't get an error.

Greetings Michael


Top
 Profile  
 
 Post subject: Re: Why the subclass also is fetched when querying on its super
PostPosted: Fri Jul 24, 2009 7:10 pm 
Regular
Regular

Joined: Tue Feb 17, 2009 3:36 pm
Posts: 78
Urmech wrote:
Hi,
Quote:
@MappedSuperclass
@Entity


what do you want to do with this?
Have you looked at your db- scheme?

Normally you use @Entity or @MappedSuperclass not both.
http://java.sun.com/javaee/5/docs/api/j ... class.html

Quote:
Designates a class whose mapping information is applied to the entities that inherit from it. A mapped superclass has no separate table defined for it.


So it is no surprise, that the subclass is fetched, the superclass has no table! I assume @Entity is ignored, if you don't get an error.

Greetings Michael


The information seems to be correct. Hoever, the application can't be brought up due to the following error after I remove the @Entity annotation mark:
Quote:
Caused by: java.lang.NullPointerException
at org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:416)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:309)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1148)

I can't explain why it complains null pointer during the server starting up.


Last edited by wei725 on Sat Jul 25, 2009 9:05 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Why the subclass also is fetched when querying on its super
PostPosted: Sat Jul 25, 2009 5:33 pm 
Regular
Regular

Joined: Thu Apr 14, 2005 10:39 am
Posts: 115
Have you tried to drop and recreate you database?
How does your db tables looks like?
Do you use "update" to adjust the db?

But I you haven't got an error before. @MappedSuperclass may be ignored.

Can't Imagine, what this NullPointerException means.

Greetings Michael


Top
 Profile  
 
 Post subject: Re: Why the subclass also is fetched when querying on its super
PostPosted: Sat Jul 25, 2009 9:03 pm 
Regular
Regular

Joined: Tue Feb 17, 2009 3:36 pm
Posts: 78
Urmech wrote:
Have you tried to drop and recreate you database?
How does your db tables looks like?
Do you use "update" to adjust the db?


I fail to see any connection between what you stated above and the null point or any related issue.


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