[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-2225) NPE when eager fetching joined component with native SQL query
Mihail Fridliand (JIRA)
noreply at atlassian.com
Wed Jul 25 06:31:53 EDT 2007
[ http://opensource.atlassian.com/projects/hibernate/browse/HHH-2225?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_27605 ]
Mihail Fridliand commented on HHH-2225:
---------------------------------------
This seems to be a general problem when using components in the native SQL. Suppose, the have class "A" containing a component class "Component" containing a collection of "B"
public class A {
private Long oid;
private Component comp = new Component();
public Component getComp() {return comp;}
public void setComp(Component comp) {this.comp = comp;}
public Long getOid() { return oid;}
public void setOid(Long oid) {this.oid = oid;}
}
public class Component {
private Set collection = new HashSet(0);
public Set getCollection() { return collection;}
public void setCollection(Set collection) { this.collection = collection;}
}
public class B {
private Long oid;
public Long getOid() { return oid;}
public void setOid(Long oid) {this.oid = oid;}
}
the mappings test1.hbm.xml:
<hibernate-mapping>
<class name="A" table="A" >
<id name="oid" type="java.lang.Long">
<column name="OID" />
<generator class="assigned" />
</id>
<component name="comp" class="Component">
<set name="collection" inverse="true" cascade="all">
<key>
<column name="A_OID" not-null="true" />
</key>
<one-to-many class="B" />
</set>
</component>
</class>
<class name="B" table="B" >
<id name="oid" type="java.lang.Long">
<column name="OID" />
<generator class="assigned" />
</id>
</class>
<resultset name="AandB">
<return alias="a" class="A"/>
<return-join alias="ac" property="a.comp"/>
<return-join alias="b" property="ac.collection"/>
</resultset>
<sql-query name="getAandB" resultset-ref="AandB" >
<![CDATA[
select DISTINCT {a.*},{b.*} from A a join B b on a.OID = b.A_OID
]]>
</sql-query>
</hibernate-mapping>
For this simple case I get a NPE
java.lang.NullPointerException
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.processJoinReturn(SQLQueryReturnProcessor.java:434)
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.processReturn(SQLQueryReturnProcessor.java:338)
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.process(SQLQueryReturnProcessor.java:148)
at org.hibernate.loader.custom.sql.SQLCustomQuery.<init>(SQLCustomQuery.java:64)
at org.hibernate.engine.query.NativeSQLQueryPlan.<init>(NativeSQLQueryPlan.java:43)
at org.hibernate.engine.query.QueryPlanCache.getNativeSQLQueryPlan(QueryPlanCache.java:114)
at org.hibernate.impl.SessionFactoryImpl.checkNamedQueries(SessionFactoryImpl.java:444)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:351)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1291)
at com.dcx.ivkmds.fwk.pc.hibernate.HibernateUtil.rebuildSessionFactory(HibernateUtil.java:191)
at com.dcx.ivkmds.fwk.pc.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:59)
The NPE is caused by the following code in the SQLQueryReturnProcessor class:
private void processJoinReturn(NativeSQLQueryJoinReturn fetchReturn) {
.....
// If this return's alias has not been processed yet, do so b4 further processing of this return
if ( !alias2Persister.containsKey( ownerAlias ) ) {
NativeSQLQueryNonScalarReturn ownerReturn = ( NativeSQLQueryNonScalarReturn ) alias2Return.get(ownerAlias);
processReturn( ownerReturn );
}
SQLLoadable ownerPersister = ( SQLLoadable ) alias2Persister.get( ownerAlias );
Type returnType = ownerPersister.getPropertyType( fetchReturn.getOwnerProperty() );
.....
When the property ac.collection is processed, there is no for the component Component (ownerAlias = "ac"). Therefore processReturn(ownerReturn) is invoked. But processReturn ignores component types. Then
alias2Persister.get(ownerAlias)
still have no entry for the ownerAlias => ownerPersister is null => NullPointerException at the next line.
If I change the resultset to (my first try)
<resultset name="AandB">
<return alias="a" class="A"/>
<return-join alias="b" property="a.comp.collection"/>
</resultset>
and try to relate to the collection of B's directly, I get
org.hibernate.HibernateException: Owner alias [a.comp] is unknown for alias [b]
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.processJoinReturn(SQLQueryReturnProcessor.java:424)
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.processReturn(SQLQueryReturnProcessor.java:338)
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.process(SQLQueryReturnProcessor.java:148)
at org.hibernate.loader.custom.sql.SQLCustomQuery.<init>(SQLCustomQuery.java:64)
What is wrong ??
> NPE when eager fetching joined component with native SQL query
> --------------------------------------------------------------
>
> Key: HHH-2225
> URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-2225
> Project: Hibernate3
> Issue Type: Bug
> Components: query-sql
> Affects Versions: 3.2.0.ga
> Reporter: Christian Bauer
> Priority: Minor
>
> Item -> many-to-one -> User -> joined component -> billingAddress
> This:
> result = session.createSQLQuery("select {i.*}, {u.*}, {ba.*} from ITEM i" +
> " join USERS u on i.SELLER_ID = u.USER_ID" +
> " left join BILLING_ADDRESS ba on u.USER_ID = ba.USER_ID" +
> " where u.USERNAME = :uname")
> .addEntity("i", Item.class)
> .addJoin("u", "i.seller")
> .addJoin("ba", "u.billingAddress")
> fails with:
> java.lang.NullPointerException
> at org.hibernate.loader.DefaultEntityAliases.<init>(DefaultEntityAliases.java:37)
> at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.generateCustomReturns(SQLQueryReturnProcessor.java:283)
> at org.hibernate.loader.custom.sql.SQLCustomQuery.<init>(SQLCustomQuery.java:129)
> at org.hibernate.engine.query.NativeSQLQueryPlan.<init>(NativeSQLQueryPlan.java:43)
> at org.hibernate.engine.query.QueryPlanCache.getNativeSQLQueryPlan(QueryPlanCache.java:114)
> at org.hibernate.impl.AbstractSessionImpl.getNativeSQLQueryPlan(AbstractSessionImpl.java:137)
> at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
> at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:150)
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira
More information about the hibernate-issues
mailing list