Uploaded image for project: 'JBoss Enterprise Application Platform 4 and 5'
  1. JBoss Enterprise Application Platform 4 and 5
  2. JBPAPP-3384

HHH-4257 map key type no longer inferred correctly, throws exception at runtime

    XMLWordPrintable

Details

    • Release Notes
    • Workaround Exists
    • Hide

      Manually define the type like so

      @MapKey(type = @Type(type = "org.hibernate.type.EnumType",
      parameters =

      { @Parameter(name = "enumClass", value = "com.domain.project.MyEnum"), @Parameter(name = "type", value = "4") }

      )
      )

      Show
      Manually define the type like so @MapKey(type = @Type(type = "org.hibernate.type.EnumType", parameters = { @Parameter(name = "enumClass", value = "com.domain.project.MyEnum"), @Parameter(name = "type", value = "4") } ) )

    Description

      Related to: http://opensource.atlassian.com/projects/hibernate/browse/HHH-4257

      Description from HHH-4257
      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      after upgrading from annotations 3.3.0GA to 3.3.1GA, my mapped collection (ManyToMany) is broken. i've identified the commit that broke it:

      http://fisheye.jboss.com/browse/Hibernate/annotations/trunk/src/java/org/hibernate/cfg/annotations/MapBinder.java?r1=12963&r2=14425

      The issue is that unless the type is given now expliticly in the @MapKey's @Type, the code now assumes a 'Serializable' type, and attempts to deserialize an object stream from the database column value. the exact exception is:

      java.io.StreamCorruptedException: invalid stream header
      at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:764)
      at java.io.ObjectInputStream.<init>(ObjectInputStream.java:277)
      at org.hibernate.util.SerializationHelper$CustomObjectInputStream.<init>(SerializationHelper.java:252)
      at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:209)
      at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:240)
      at org.hibernate.type.SerializableType.fromBytes(SerializableType.java:82)
      at org.hibernate.type.SerializableType.get(SerializableType.java:39)
      at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)
      ...

      The fix is simple: if @Type is NOT given in the @MapKey annotation, keep the old behavior which at least tries to set the type, even if it may not work in all situations. The attached patch fixes the problem for me.

      In my particular situation, the map key type is a java.lang.Enum. Here's what my annotated property looks like:

      @ManyToMany
      @JoinTable(
      name="chargeback_owner"
      ,joinColumns=@JoinColumn(name="person_id",referencedColumnName="person_id")
      ,inverseJoinColumns=@JoinColumn(name="owner_person_id")
      )
      @MapKey(columns=@Column(name="owner_type_cd"),targetElement=ChargebackOwnerType.class)
      @Enumerated(EnumType.STRING)
      @Sort(type=SortType.NATURAL)
      private SortedMap<ChargebackOwnerType,Person> owners = new TreeMap<ChargebackOwnerType, Person>();
      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++

      Attachments

        1. hibernate-annotations.jar
          274 kB
        2. MediaType.java
          0.1 kB
        3. Person.java
          1 kB
        4. QOSClassIdentifier.java
          0.1 kB

        Activity

          People

            shaozliu Strong Liu(刘少壮) (Inactive)
            sjmenden_jira Samuel Mendenhall (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: