-->
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: Native SQL Query and Inheritance mapping fails
PostPosted: Fri Jul 27, 2007 4:27 pm 
Beginner
Beginner

Joined: Thu Apr 19, 2007 9:52 am
Posts: 27
Hi,

We have three class Interested Party, BusinessEntity and Employer. Interested Party is the base class. BusinessEntity extends Interested Party and third POJO Employer extends BusinessEntity. Now when we map these classes in hibernate we have to use joined subclass so that we can implement inheritance mapping.

We have a requirement due to which we need to use native SQL Queries to do some findBy's. And we do findBy's on BusinessEntity or Employer. When we run findBy's on Employer it works great but when we try findBy's on BusinessEntity it fails with exception

Databases i tried this with are DB2 and Oracle

Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:2223)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:289)
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1695)
at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:152)
at BusinessEntityHome.findBySQLFEIN(BusinessEntityHome.java:150)
at TestSearchAPI.main(TestSearchAPI.java:13)
Caused by: COM.ibm.db2.jdbc.DB2Exception: [IBM][JDBC Driver] CLI0611E Invalid column name. SQLSTATE=S0022
at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.rsException(Unknown Source)
at COM.ibm.db2.jdbc.app.DB2ResultSet.findColumn(Unknown Source)
at COM.ibm.db2.jdbc.app.DB2ResultSet.getInt(Unknown Source)
at org.hibernate.type.IntegerType.get(IntegerType.java:28)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:189)
at org.hibernate.loader.Loader.getInstanceClass(Loader.java:1442)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1284)
at org.hibernate.loader.Loader.getRow(Loader.java:1206)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:580)
at org.hibernate.loader.Loader.doQuery(Loader.java:701)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2220)
... 8 more


Debugging the hibernate code I figured out that it fails as hibernate is looking for clazz_ in the resultset.

To add to that if you try to hard code the query and run it as NamedQuery it runs great but the generated query has something like this included

Hibernate: select businessen0_.IP_ID as ID0_, businessen0_1_.NAME as NAME0_, businessen0_.FEIN as FEIN1_, businessen0_2_.EAN as EAN2_, case when businessen0_2_.IP_ID is not null then 2 when businessen0_.IP_ID is not null then 1 end as clazz_ from DB2.BUSINESS_ENTITY businessen0_ inner join DB2.INTERESTED_PARTY businessen0_1_ on businessen0_.IP_ID=businessen0_1_.ID left outer join DB2.EMPLR businessen0_2_ on businessen0_.IP_ID=businessen0_2_.IP_ID where businessen0_.FEIN=?


So I tried to run my SQLQuery like this

select INTERESTED_PARTY.*, BUSINESS_ENTITY.*, 1 as clazz_ from BUSINESS_ENTITY, INTERESTED_PARTY where BUSINESS_ENTITY.IP_ID = INTERESTED_PARTY.ID AND BUSINESS_ENTITY.FEIN=:fein

and it runs. But adding 1 as clazz_ is something which is not a proper way for a SQL Query.

Can someone suggest me a proper way to run it or is it a bug with hibernate.

_________________
Regards,
Mahen
http://www.discoverabout.net
http://funnfacts.discoverabout.net


Top
 Profile  
 
 Post subject: Native SQL Query and Inheritance mapping fails
PostPosted: Mon Jul 30, 2007 12:01 pm 
Newbie

Joined: Thu Jun 02, 2005 7:19 am
Posts: 1
Hi,

i've the same problem using inheritance and native Queries. The resulting Query from Hibernate which is then send to DB is not correct.

F.e. We have a Customer (Kunde) - Table and an Person Table which has more than one inherited tables such as Natperson ... and so on.

The Query i wrote looks like:
"select {d.*},{k.*} from Kunde k inner join Person d on k.personid = d.personid"
and i added the two entities:

query.addEntity("d",Person.class);
query.addEntity("k",Kunde.class);

The Query which was than made from Hibernate looked like:

SELECT d.personid AS personid327_0_, d.anredeid AS anredeid327_0_,
d.old_instanzid AS old3_327_0_, d.mandantid AS mandantid327_0_,
d.persontypid AS personty5_327_0_, d_1_.mandantid AS mandantid328_0_,
d_1_.personid AS personid328_0_, d_1_.name1 AS name3_328_0_,
d_1_.NAME AS name328_0_, d_2_.spracheid AS spracheid329_0_,
d_2_.mandantid AS mandantid329_0_, d_2_.personid AS personid329_0_,
d_3_.personid AS personid330_0_, d_4_.subsystemid AS subsyste2_331_0_,
d_4_.subsyskey AS subsyskey331_0_, d_4_.mandantid AS mandantid331_0_,
d_4_.gueltig_bis AS gueltig5_331_0_, d_4_.personid AS personid331_0_,
d_4_.gueltig_ab AS gueltig6_331_0_,
d_5_.mediauntertypid AS mediaunt2_332_0_,
d_5_.subsystemid AS subsyste3_332_0_,
d_5_.subsyskey AS subsyskey332_0_, d_5_.nationid AS nationid332_0_,
d_5_.gueltig_bis AS gueltig6_332_0_, d_5_.personid AS personid332_0_,
d_5_.gueltig_ab AS gueltig7_332_0_, d_6_.mandantid AS mandantid333_0_,
d_6_.personid AS personid333_0_, d_7_.personid AS personid334_0_,
d_8_.spracheid AS spracheid335_0_, d_8_.mandantid AS mandantid335_0_,
d_8_.moechteanschnitt AS moechtea4_335_0_,
d_8_.personid AS personid335_0_, d_9_.spracheid AS spracheid336_0_,
d_9_.mandantid AS mandantid336_0_, d_9_.personid AS personid336_0_,
d_10_.gebdatum AS gebdatum337_0_,
d_10_.fremdschluessel AS fremdsch3_337_0_,
d_10_.mandantid AS mandantid337_0_, d_10_.personid AS personid337_0_,
d_10_.NAME AS name337_0_, d_10_.titelid AS titelid337_0_,
d_10_.vorname AS vorname337_0_, d_11_.sdatum AS sdatum338_0_,
d_11_.bemerkung AS bemerkung338_0_, d_11_.mandantid AS mandantid338_0_,
d_11_.personid AS personid338_0_,
CASE
WHEN d_6_.personid IS NOT NULL
THEN 6
WHEN d_7_.personid IS NOT NULL
THEN 7
WHEN d_2_.personid IS NOT NULL
THEN 2
WHEN d_3_.personid IS NOT NULL
THEN 3
WHEN d_4_.personid IS NOT NULL
THEN 4
WHEN d_5_.personid IS NOT NULL
THEN 5
WHEN d_8_.personid IS NOT NULL
THEN 8
WHEN d_9_.personid IS NOT NULL
THEN 9
WHEN d_11_.personid IS NOT NULL
THEN 11
WHEN d_1_.personid IS NOT NULL
THEN 1
WHEN d_10_.personid IS NOT NULL
THEN 10
WHEN d.personid IS NOT NULL
THEN 0
END AS clazz_0_,
k.kundeid AS kundeid461_1_, k.stammnummer AS stammnum2_461_1_,
k.fremdsyskey AS fremdsys3_461_1_, k.sdatum AS sdatum461_1_,
k.bezeichnung AS bezeichn5_461_1_, k.spracheid AS spracheid461_1_,
k.edatum AS edatum461_1_, k.ustdident AS ustdident461_1_,
k.mandantid AS mandantid461_1_, k.personid AS personid461_1_,
k.kundetypid AS kundetypid461_1_, k.fremdsystemid AS fremdsy12_461_1_
FROM kunde k INNER JOIN person d ON k.personid = d.personid


So as you can see, Hibernate recognizes all inherited tables/objects from Person (in query named as d_1_...d_10_..) but Hibernate did not extend the from clause as needed. So the Query must fail with unknown aliases (d_1...d_10_) for tables.
Why does Hibernate also try to fetch and query inherited tables while using native queries as it's also using HQL.
Is there a way to disable this? In case inherited tables/objects have not to be fetched which would also be faster.

Thanks,
Heiko


Top
 Profile  
 
 Post subject: Re: Native SQL Query and Inheritance mapping fails
PostPosted: Fri Nov 06, 2009 6:19 pm 
Newbie

Joined: Mon Nov 12, 2007 6:51 pm
Posts: 1
Hey, this is exactly what I am seeing. Also, the only documentation that I can find in regards to inheritance use with native SQL is this:

16.1.6. Handling inheritance

Native SQL queries which query for entities that are mapped as part of an inheritance must include all properties for the baseclass and all its subclasses.

That's it. That's all it says. Could we get an example added to the documentation. Does anyone even look at these forums from Hibernate?


Top
 Profile  
 
 Post subject: Re: Native SQL Query and Inheritance mapping fails
PostPosted: Tue Sep 20, 2011 3:51 pm 
Newbie

Joined: Tue Sep 20, 2011 3:48 pm
Posts: 1
Hello.

Does anybody have a solution for this issue? I have been having the same problem while trying to create a native SQL query.


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.