[
http://opensource.atlassian.com/projects/hibernate/browse/HHH-5832?page=c...
]
Erich Heard updated HHH-5832:
-----------------------------
Attachment: hhh-5832.zip
Gail, I've attached a standalone eclipse project that can recreate the error with
3.6.0. You can test it by passing division codes in to the program (NA, EU, ASIA are
valid). I've also attached the class I altered. If you look down to the bottom, you
will see the method I overrode commented out. You can uncomment this and the app should
run fine. Let me know if you need anything else.
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, 3.6.0
Environment: Hibernate version 3.5.1 and Oracle 10g.
Reporter: Erich Heard
Attachments: hhh-5832.zip
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....
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira