-->
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.  [ 4 posts ] 
Author Message
 Post subject: Selecting only the parent class from HQL
PostPosted: Sun Nov 06, 2005 1:42 am 
Beginner
Beginner

Joined: Wed Jun 15, 2005 2:00 pm
Posts: 38
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0.5

Mapping documents:
Code:
<class name="GenericObject" table="GENERIC_OBJECTS" lazy="true" polymorphism="explicit">
      <id name="id" column="id" type="integer" unsaved-value="0">
         <generator class="native" />
      </id>
      <discriminator type="string" column="classID" />
      <property name="classID" column="classID" type="integer" not-null="true" insert="false" update="false"/>
      <property name="name" column="name" type="string" not-null="true" />
      <property name="description" column="description" type="string" />
      <property name="dateCreated" column="dateCreated" type="timestamp" not-null="true" />
      <property name="dateModified" column="dateModified" type="timestamp" />

      <subclass name="User" discriminator-value="1" lazy="false" >
         <join table="GENERIC_USERS">
            <key column="id" />
            <property name="loginName" type="string"/>
            <property name="password" type="string"/>
         </join>
      </subclass>
      
      <subclass name="UserGroup" discriminator-value="2" lazy="false">
         <join table="GENERIC_GROUPS">
            <key column="id" />
            <property name="roleGroup" type="boolean"/>
         </join>
      </subclass>
      
   </class>


Code between sessionFactory.openSession() and session.close():
Code:
Query q = s.createQuery("select g from GenericObject g");
      GenericObject loginName = (GenericObject) q.uniqueResult();



The generated SQL (show_sql=true):
Code:
Hibernate: /* select g from GenericObject g */ select genericobj0_.id as id, genericobj0_.classID as classID0_, genericobj0_.name as name0_, genericobj0_.description as descript4_0_, genericobj0_.dateCreated as dateCrea5_0_, genericobj0_.dateModified as dateModi6_0_, genericobj0_1_.loginName as loginName1_, genericobj0_1_.password as password1_, genericobj0_2_.roleGroup as roleGroup2_, genericobj0_.classID as classID from GENERIC_OBJECTS genericobj0_ left outer join GENERIC_USERS genericobj0_1_ on genericobj0_.id=genericobj0_1_.id left outer join GENERIC_GROUPS genericobj0_2_ on genericobj0_.id=genericobj0_2_.id


Hi all,

As per above, I have a base class, GenericObject, extended by 2 sub-classes User and UserGroup. I've set the polymorphism property for GenericObject to "explicit".

What I thought this would do is that if I executed a HQL like 'from GenericObject g', it should just return me a List of GenericObjects, querying only the GENERIC_OBJECTS table.

However from the generated SQL, it still seems to be doing the joins to the sub-class tables :-(

How do I force Hibernate to just return the GenericObjects, instead of trying to get the specifics of each sub-class? Is it possible to do this at all?

Also, one more other observation:
Can the discriminator type be anything else other than "string"? I'm asking because I've set the type to "integer" initially, but Hibernate complains about this.

Code:
%%%% Error Creating SessionFactory %%%%
org.hibernate.MappingException: Could not format discriminator value to SQL string
   at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:273)
   at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55)
   at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:211)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1005)
   at project.hibernate.SessionFactory.currentSession(SessionFactory.java:48)
   at HibernateTest.main(HibernateTest.java:17)
Caused by: java.lang.NumberFormatException: For input string: "project.objects.GenericObject"
   at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
   at java.lang.Integer.parseInt(Integer.java:468)
   at java.lang.Integer.<init>(Integer.java:609)
   at org.hibernate.type.IntegerType.stringToObject(IntegerType.java:53)
   at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:266)
   ... 5 more
java.lang.NullPointerException
   at project.hibernate.SessionFactory.currentSession(SessionFactory.java:55)
   at HibernateTest.main(HibernateTest.java:17)
Exception in thread "main"



Thanks in advance for any advice!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 06, 2005 11:40 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
1. I don't think polymorpihsm="explicit" is meant to be used with subclass or join-class notion. It's more used when you have plain java polymorphism and both classes are simple mapped classes - no other Hibernate biniding between.

2. Sure. You can use few other discriminators. In your case it fails because by default discriminator value is class name - which is ofcourse not integer. Set disciriminator value to your generic object to an integer and it will work.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 07, 2005 2:42 am 
Beginner
Beginner

Joined: Wed Jun 15, 2005 2:00 pm
Posts: 38
Hi alesj,

Thanks for the suggestions!

Just need some clarifications:

For 1)...
So that means there is actually no way to do what I want to do? Anyway, I've figured a way to do this already:
1. Declare another class which maps to the same table as the root class (GENERIC_OBJECTS) and select that class instead whenever I want to query for generic objects.

That leaves me with the problem of deleting the generic objects cleanly (including the sub-class data).
I think we'll need either a database trigger or a stored procedure for this. Can't really see any other way to do this.

For 2)...
Not sure what you mean, because as I've said, I've originally set the discrimainator-value attribute of the generic object to type "integer". That was what got me the error message in the first place?
And that was why I changed it to "string" instead.


alesj wrote:
1. I don't think polymorpihsm="explicit" is meant to be used with subclass or join-class notion. It's more used when you have plain java polymorphism and both classes are simple mapped classes - no other Hibernate biniding between.

2. Sure. You can use few other discriminators. In your case it fails because by default discriminator value is class name - which is ofcourse not integer. Set disciriminator value to your generic object to an integer and it will work.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 07, 2005 2:56 am 
Regular
Regular

Joined: Sat Nov 05, 2005 5:33 am
Posts: 70
Location: Linz, Austria
everbright wrote:
For 2)...
Not sure what you mean, because as I've said, I've originally set the discrimainator-value attribute of the generic object to type "integer". That was what got me the error message in the first place?
And that was why I changed it to "string" instead.

alesj wrote:
2. Sure. You can use few other discriminators. In your case it fails because by default discriminator value is class name - which is ofcourse not integer. Set disciriminator value to your generic object to an integer and it will work.


I think your original mapping specified the type of the discriminator explicitly:
Code:
      <discriminator [color=red]type="string"[/color] column="classID" />


I'd let Hibernate find the type from the property itself.
IMHO, if classID is an int or Integer, it should work out automatically if you use the following:
Code:
      <discriminator column="classID" />


Alternatively, you might want to specify integer yourself:
Code:
      <discriminator [color=red]type="integer"[/color] column="classID" />


hth,

_________________
hth,
Heinz
Don't forget to rate if this helped


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