[hibernate-issues] [Hibernate-JIRA] Created: (HHH-5832) JPA Query and IdClass Causing NullPointerException

Erich Heard (JIRA) noreply at atlassian.com
Mon Jan 10 23:35:05 EST 2011


JPA Query and IdClass Causing NullPointerException
--------------------------------------------------

                 Key: HHH-5832
                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-5832
             Project: Hibernate Core
          Issue Type: Bug
          Components: metamodel
    Affects Versions: 3.5.1
         Environment: Hibernate version 3.5.1 and Oracle 10g.
            Reporter: Erich Heard


We had an issue when building queries dynamically with JPA objects when we accessed an @Id property of an entity class that used @IdClass.  Under these conditions, Path.get( ) would return null.  Here is a short illustration:


The entity was specified like:

@Entity
//  snipped more annotations
@IdClass( RetailerId.class )
public class Retailer {
	@Id
	private String retailerId;
	public String getRetailerId( ) { return this.retailerId; }
	public void setRetailerId( String value ) { this.retailerId = value; }
	
	@Id
	private String divisionCode;
	public String getDivisionCode( ) { return this.divisionCode; }
	public void setDivisionCode( String value ) { this.divisionCode = value; }
        
        //  snipped other attributes
}



And the ID class was specified like:

@Embeddable
public class RetailerId implements Serializable {
	private static final long serialVersionUID = -2639451633891197777L;

	@Column( name = "RETLR_ID", nullable = false, length = 7 )
	private String retailerId;
	public String getRetailerId( ) { return this.retailerId; }
	public void setRetailerId( String value ) { this.retailerId = value; }
	
	@Column( name = "DIV_CDE", nullable = false, length = 2 )
	private String divisionCode;
	public String getDivisionCode( ) { return this.divisionCode; }
	public void setDivisionCode( String value ) { this.divisionCode = value; }
        
        //  snipped implementations of Serializable methods
}



The code that caused the error condition:

CriteriaQuery<RetailerConfigurationPreference> query = cb.createQuery(RetailerConfigurationPreference.class);
Root<RetailerConfigurationPreference> root = query.from(RetailerConfigurationPreference.class);

Predicate predicate = root.get("retailer").get("retailerId").in(retailerIds);



In this case, the 'get("retailerId")' would return null.  To get around this issue, I altered the class org.hibernate.ejb.metamodel.AbstractIdentifiableType and overrode its super method getAttribute() to check the ID class for the attribute before deferring to its superclass.

@Override
public Attribute<? super X, ?> getAttribute(String name) {
	if( this.idClassAttributes != null ) {
		Attribute<? super X, ?> attribute = null;
		Iterator<SingularAttribute<? super X, ?>> i = this.idClassAttributes.iterator( );
		
		while( i.hasNext( ) ) {
			attribute = i.next( );
			if( attribute.getName( ).equals( name ) ) return attribute;
		}
	}
	
	return super.getAttribute( name );
}


I didn't test this with EmbeddedId - that may work as is or it may need a case of its own.  This is the first time I've submitted anything here, so I apologize if it is not to form.

-- 
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