1

Hello I'm a newbie with the JPA-Framework. In my project I use JPA with EclipseLink and DerbyDB. It's all working for me fine. But then I would persist a HashMap, that's my problem.

The HashMap looks like (used in GameArea):

private HashMap<GameObject, ObjectContainer> gameObjects;

class GameArea:

@Entity
public class GameArea implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long OID;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private HashMap<GameObject, ObjectContainer> gameObjects;
}

class GameObject (the abstract object):

@MappedSuperclass
public abstract class GameObject implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long OID;
...
}

class ObjectContainer (the composite component):

@Entity
public abstract class ObjectContainer extends GameObject implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long OID;
...
}

My HashMap is a lookup table for objects from GameObject to GameObject (as a ObjectContainer).

I've got an exception:

Exception [EclipseLink-163] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: This mapping's attribute class does not match the collection class.  [class java.util.Hashtable] cannot be assigned to [class java.util.HashMap].
Mapping: org.eclipse.persistence.mappings.ManyToManyMapping[gameObjects]
Descriptor: RelationalDescriptor(GameArea --> [DatabaseTable(GAMEAREA)])

I don't know what it means... I'm not sure with the "OneToMany" relation...

Please can you help me?

1 Answers1

0

JPA does not support mapping HashMap. Interface java.util.Map should be used instead. JPA provider is free to choose which actual implementation of Map it uses. As error message tells, EclipseLink uses java.util.Hashtable.

If using Map instead of HashMap fits to requirements, then following solves problem:

  @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
  private Map<GameObject, ObjectContainer> gameObjects;

If HashMap is needed, easy workaround is to provide method that convert between persistent field gameObjects and HashMap.

Mikko Maunu
  • 41,366
  • 10
  • 132
  • 135