[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-5832) JPA Query and IdClass Causing NullPointerException
Erich Heard (JIRA)
noreply at atlassian.com
Thu Jan 13 19:14:05 EST 2011
[ http://opensource.atlassian.com/projects/hibernate/browse/HHH-5832?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=39616#action_39616 ]
Erich Heard commented on HHH-5832:
----------------------------------
Gail, I tried swapping 3.6.0 for 3.5.1 in our project, but there is some conflict between it, the version of Spring and/or JUnit that's not allowing me to quickly run my unit tests against it. I can definitely work it out when I have some time, but in looking at the source (org.hibernate.ejb.metamodel.AbstractManagedType):
3.5.1, line 128:
@SuppressWarnings({ "unchecked" })
public Attribute<? super X, ?> getAttribute(String name) {
Attribute<? super X, ?> attribute = declaredAttributes.get( name );
if ( attribute == null && getSupertype() != null ) {
attribute = getSupertype().getAttribute( name );
}
return attribute;
}
3.6.0 line 128:
@SuppressWarnings({ "unchecked" })
public Attribute<? super X, ?> getAttribute(String name) {
Attribute<? super X, ?> attribute = declaredAttributes.get( name );
if ( attribute == null && getSupertype() != null ) {
attribute = getSupertype().getAttribute( name );
}
return attribute;
}
Both versions can ultimately return null if the requested attribute is not in the declaredAttributes collection which, apparently, fields marked with @Id are not. This leads me to think that it would still be an issue in 3.6.0. My "fix" was to the descendant that was being instantiated (AbstractIdentifiableType) - I'm not sure if that is the correct place, but it was the only class that already had access to the attributes that were marked with @Id - in its idClassAttributes collection.
> 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