Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 356896

Summary: NullPointerException when executing a query with nested embeddables
Product: z_Archived Reporter: Viktor Moser <viktor.moser>
Component: EclipselinkAssignee: Nobody - feel free to take it <nobody>
Status: NEW --- QA Contact:
Severity: normal    
Priority: P2 CC: jpsoto, tom.ware
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Viktor Moser CLA 2011-09-07 04:41:07 EDT
Build Identifier: 20110615-0604

When executing a typed query with nested embeddables, a NullPointerException is thrown in InheritancePolicy.java:1381.

Assume an object of Type "GenericEntity" with embeddable "EmbeddedObject" that contains itself another embeddable, e.g. "NestedEmbeddedObject". 
A query for an object of type GenericEntity fails fails due to NPE in InheritancePolicy.java:1381.

Code sample:

    CriteriaBuilder cb = getEm().getCriteriaBuilder();
    CriteriaQuery<ExampleEntity> cq = cb.createQuery(ExampleEntity.class);
    Root<ExampleEntity> root = cq.from(ExampleEntity.class);
    List<Predicate> predicates = new ArrayList<Predicate>();

    Map<String, Object> attributes = new HashMap();
    ExampleEntity   ent = new ExampleEntity();
    //NOTE: EmbeddedObject is an embeddable that contains another embeddable, e.g. NestedEmbeddedObject
    EmbeddedObject  emb = ent.getEmbeddedObject();
    
    predicates.add(cb.equal((Expression) root.get("embeddedObject"), emb));
    
    cq.where(predicates.toArray(new Predicate[] {}));
    TypedQuery<ExampleEntity> q = getEm().createQuery(cq);

    //fails due to NPE in InheritancePolicy.java:1381
    List<ExampleEntity> results = q.getResultList();


java.lang.NullPointerException
	at org.eclipse.persistence.descriptors.InheritancePolicy.selectAllRowUsingDefaultMultipleTableSubclassRead(InheritancePolicy.java:1381)
	at org.eclipse.persistence.descriptors.InheritancePolicy.selectAllRowUsingMultipleTableSubclassRead(InheritancePolicy.java:1422)
	at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRows(ExpressionQueryMechanism.java:2548)
	at org.eclipse.persistence.queries.ReadAllQuery.executeObjectLevelReadQuery(ReadAllQuery.java:418)
	at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(ObjectLevelReadQuery.java:1097)
	at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:829)...

Reproducible: Always

Steps to Reproduce:
Assume an object of Type "GenericEntity" with embeddable "EmbeddedObject" that contains itself another embeddable, e.g. "NestedEmbeddedObject". 
A query for an object of type GenericEntity fails fails due to NPE in InheritancePolicy.java:1381.

Code sample:

    CriteriaBuilder cb = getEm().getCriteriaBuilder();
    CriteriaQuery<ExampleEntity> cq = cb.createQuery(ExampleEntity.class);
    Root<ExampleEntity> root = cq.from(ExampleEntity.class);
    List<Predicate> predicates = new ArrayList<Predicate>();

    Map<String, Object> attributes = new HashMap();
    ExampleEntity   ent = new ExampleEntity();
    //NOTE: EmbeddedObject is an embeddable that contains another embeddable, e.g. NestedEmbeddedObject
    EmbeddedObject  emb = ent.getEmbeddedObject();
    
    predicates.add(cb.equal((Expression) root.get("embeddedObject"), emb));
    
    cq.where(predicates.toArray(new Predicate[] {}));
    TypedQuery<ExampleEntity> q = getEm().createQuery(cq);

    //fails due to NPE in InheritancePolicy.java:1381
    List<ExampleEntity> results = q.getResultList();


java.lang.NullPointerException
	at org.eclipse.persistence.descriptors.InheritancePolicy.selectAllRowUsingDefaultMultipleTableSubclassRead(InheritancePolicy.java:1381)
	at org.eclipse.persistence.descriptors.InheritancePolicy.selectAllRowUsingMultipleTableSubclassRead(InheritancePolicy.java:1422)
	at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRows(ExpressionQueryMechanism.java:2548)
	at org.eclipse.persistence.queries.ReadAllQuery.executeObjectLevelReadQuery(ReadAllQuery.java:418)
	at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(ObjectLevelReadQuery.java:1097)
	at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:829)...
Comment 1 Tom Ware CLA 2011-09-22 11:38:21 EDT
Setting target and priority.  See the following page for the meanings of these fields:

http://wiki.eclipse.org/EclipseLink/Development/Bugs/Guidelines

Community: Please vote for this bug if it is important to you.  Votes are one of the main criteria we use to determine which bugs to fix next.
Comment 2 Viktor Moser CLA 2011-09-26 07:21:14 EDT
PLEASE NOTE:

After a deeper analysis I figured that the problem was not caused by the use of nested embeddables - the exception was raised because we used embeddables in a superclass of another entity class (inheritance strategy: JOINED).
 
In this case, queries (built with the criteria API) on the superclass failed when using an embeddable as search criteria (queries on the subclass worked).
Our workaround was to wrap the embedded objects in the superclass. I can recommend this approach in general when using the criteria API because otherwise all non-set fields of an embeddable are compared by "is null" which is presumably not the wanted behavior in most use cases.
Comment 3 Juan Pablo Soto CLA 2011-11-17 15:35:21 EST
Another example:

	@SuppressWarnings("unchecked")
	public <ENTITY> ENTITY getTramiteProxyByTramite(Class<ENTITY> clazz, CTramite tramite) throws CException {
		try {
			Query query = getEntityManager().createQuery(
					"SELECT alias FROM " + clazz.getSimpleName()
					+ " alias WHERE alias.tramite.id = :tramiteId AND alias.fechaFinVigencia is null");
			query.setParameter("tramiteId", tramite.getId());
			return (ENTITY) query.getSingleResult();
		} catch (NoResultException e) {
			return null;
		} catch (Exception ex) {
			throw new CException(1, ex);
		}
	}


ADVERTENCIA: java.lang.NullPointerException
	at org.eclipse.persistence.descriptors.InheritancePolicy.selectAllRowUsingDefaultMultipleTableSubclassRead(InheritancePolicy.java:1381)
	at org.eclipse.persistence.descriptors.InheritancePolicy.selectAllRowUsingMultipleTableSubclassRead(InheritancePolicy.java:1422)
	at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRows(ExpressionQueryMechanism.java:2548)
	at org.eclipse.persistence.queries.ReadAllQuery.executeObjectLevelReadQuery(ReadAllQuery.java:418)
	at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(ObjectLevelReadQuery.java:1097)
	at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:829)
	at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1056)
	at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:390)
	at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1144)
	at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2863)
	at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1501)
	at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1483)
	at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1457)
	at org.eclipse.persistence.internal.jpa.EJBQueryImpl.executeReadQuery(EJBQueryImpl.java:485)
	at org.eclipse.persistence.internal.jpa.EJBQueryImpl.getSingleResult(EJBQueryImpl.java:773)
	at ar.gov.conicet.apps.global.dao.jpa.CDAOUtil.getTramiteProxyByTramite(CDAOUtil.java:59)
Comment 4 Eclipse Webmaster CLA 2022-06-09 10:23:57 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink