[hibernate-issues] [Hibernate-JIRA] Created: (ANN-723) Impossible to bind a SortedMap collection

Alexandre Dutra (JIRA) noreply at atlassian.com
Wed Apr 2 12:21:32 EDT 2008


Impossible to bind a SortedMap collection
-----------------------------------------

                 Key: ANN-723
                 URL: http://opensource.atlassian.com/projects/hibernate/browse/ANN-723
             Project: Hibernate Annotations
          Issue Type: Bug
    Affects Versions: 3.3.0.ga
         Environment: hibernate: 3.2.5.ga
hibernate-annotations: 3.3.0.ga 
database: HSQL 1.8.0.7
            Reporter: Alexandre Dutra
            Priority: Minor


See http://forum.hibernate.org/viewtopic.php?t=976437

I have the following collection declared in a persistent class:

    @CollectionOfElements
    @Sort(type = SortType.NATURAL)
    @JoinTable(name = "delivery_option_price", joinColumns = @JoinColumn(name = "delivery_option_id"))
    private SortedMap<Float, MonetaryAmount> prices = new TreeMap<Float, MonetaryAmount>();

Hibernate fails to bind it to a database table. The stack trace is:

java.lang.NullPointerException
at org.hibernate.cfg.annotations.MapBinder.bindKeyFromAssociationTable(MapBinder.java:129)
at org.hibernate.cfg.annotations.MapBinder.access$000(MapBinder.java:53)
at org.hibernate.cfg.annotations.MapBinder$1.secondPass(MapBinder.java:83)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:43)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1130)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:316)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1286)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:915) 
...

A workaround to this is to explicitly specify the map key class by adding a org.hibernate.annotations.MapKey annotation:

@org.hibernate.annotations.MapKey(targetElement = Float.class, columns = @Column(name = "WEIGHT"))

Browsing the source code, I realized that the NullPointerException is being raised beacause the following method returns null in org.hibernate.annotations.common.reflection.java.JavaXCollectionType:

public XClass getMapKey() {
   return new TypeSwitch<XClass>() {
      @Override
      public XClass caseParameterizedType(ParameterizedType parameterizedType) {
         if ( getCollectionClass().isAssignableFrom( Map.class ) ) {
            return toXClass( parameterizedType.getActualTypeArguments()[0] );
         }
         return null;
      }
   }.doSwitch( approximate() );
}

More specifically, the following expression seems misconstructed:

getCollectionClass().isAssignableFrom( Map.class )

The correct expression would be:

Map.class.isAssignableFrom( getCollectionClass() )

At least if, as I understand it, the goal is to determine whether the collection class is or is not a Map in anyone one of its implementations. The same applies to the getElementClass() method, in the same class.

If my analyzis is correct, the bug might be very easy to fix, but maybe I'm underestimating its impacts.

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