[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-2225) NPE when eager fetching joined component with native SQL query

Eelco Hillenius (JIRA) noreply at atlassian.com
Mon Apr 21 22:23:33 EDT 2008


    [ http://opensource.atlassian.com/projects/hibernate/browse/HHH-2225?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_30035 ] 

Eelco Hillenius commented on HHH-2225:
--------------------------------------

I just bumped my head against this as well. Using 3.2.6.ga, I the following exception:

java.lang.NullPointerException
     at org.hibernate.loader.DefaultEntityAliases.intern(DefaultEntityAliases.java:133)
     at org.hibernate.loader.DefaultEntityAliases.getSuffixedPropertyAliases(DefaultEntityAliases.java:106)
     at org.hibernate.loader.DefaultEntityAliases.<init>(DefaultEntityAliases.java:52)
     at org.hibernate.loader.ColumnEntityAliases.<init>(ColumnEntityAliases.java:16)
     at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.generateCustomReturns(SQLQueryReturnProcessor.java:174)
     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:152)

The relevant classes are:

@Entity
@Table(name = "site_node_permission")
public class SiteNodePermission extends AbstractDomainObject {

	@GeneratedValue
	@Id
	@Column(name = "site_node_permission_id")
	private Long id;

	@ManyToOne
	@JoinColumn(name = "fk_site_node_id")
	private SiteNode siteNode;

	@ManyToOne
	@JoinColumn(name = "fk_user_id")
	private User user;

	@Column(name = "permissions")
	private int permissions = 0;

	@Formula("(select bit_or(p.permissions) from site_node_permission p, site_node n, site_node na where p.fk_user_id = fk_user_id and n.site_node_id = fk_site_node_id and na.site_node_id = p.fk_site_node_id and na.nested_set_thread_id = n.nested_set_thread_id and n.nested_set_l_index between na.nested_set_l_index and na.nested_set_r_index group by p.fk_user_id)")
	private Integer joinedPermissions = null;

and:

@Entity
@Table(name = "site_node")
public class SiteNode extends AbstractDomainObject {

	@Id
	@GeneratedValue
	@Column(name = "site_node_id")
	private Long id;

	@Column(name = "name", length = 128, nullable = false)
	@StringValidator(min = 2)
	private String name;

	@ManyToOne()
	@JoinColumn(name = "parent_site_node_id")
	private SiteNode parent;

	@OneToMany(cascade = CascadeType.ALL, mappedBy = "parent")
	@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
	@JoinColumn(name = "parent_site_node_id")
	private Set<SiteNode> children = new HashSet<SiteNode>();

	@Column(name = "nested_set_thread_id", nullable = false)
	private Long nestedSetThreadId;

	@Column(name = "nested_set_l_index", nullable = false)
	private Integer nestedSetLeftIndex;

	@Column(name = "nested_set_r_index", nullable = false)
	private Integer nestedSetRightIndex;

	@OneToMany(cascade = CascadeType.ALL, mappedBy = "siteNode")
	@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
	@JoinColumn(name = "fk_site_node_id")
	private Set<SiteNodePermission> permissions = new HashSet<SiteNodePermission>();

and the query, executed as (native) SQLQuery:

select p.* from site_node_permission p, site_node n where n.site_node_id = p.fk_site_node_id and n.nested_set_thread_id = :threadId and :left between n.nested_set_l_index and n.nested_set_r_index and n.nested_set_l_index = (select max(na.nested_set_l_index) from site_node_permission as pa join site_node as na on na.site_node_id = pa.fk_site_node_id where n.nested_set_thread_id = na.nested_set_thread_id and :left between na.nested_set_l_index and na.nested_set_r_index and pa.fk_user_id = p.fk_user_id group by pa.fk_user_id)

When I replace the Formula annotation of SiteNodePermission with Transient, the query works, and the Formula doesn't generate problems for non-native queries, so it would be my conclusion, like DK said, that Formula and native queries somehow bit each other.


> 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