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!