[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-5832) JPA Query and IdClass Causing NullPointerException
Steve Ebersole (JIRA)
noreply at atlassian.com
Wed Aug 31 23:03:05 EDT 2011
[ http://opensource.atlassian.com/projects/hibernate/browse/HHH-5832?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=43428#comment-43428 ]
Steve Ebersole commented on HHH-5832:
-------------------------------------
Erich, wanted to say thanks a lot for the test. Unfortunately we got caught up in time crunch here on this release. But I will work on this right after I do the release tonight.
> 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
> Assignee: Steve Ebersole
> Labels: jpa2
> Fix For: 4.0.0.next
>
> Attachments: hhh-5832.zip, 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.
For more information on JIRA, see: http://www.atlassian.com/software/jira
More information about the hibernate-issues
mailing list