I am using hibernate version 5.4.6 final with JPA and Java 8.
I have a One to many bi-directional relationship between entities.
Entity uses a Map to represent items, they are as below. Full entity classes are here.
@Entity
@Access(AccessType.FIELD)
public class E_Cart {
@Id
@GeneratedValue(generator = "cartSeq",
strategy = GenerationType.SEQUENCE)
private long id;
@OneToMany(mappedBy = "cart")
@MapKey(name = "version")
private Map<String, E_CartItem> cartItemMap = new HashMap<>();
// skipping getter and setters
}
@Entity
@Access(AccessType.FIELD)
public class E_CartItem {
@Id
@GeneratedValue(generator = "cartItemSeq",
strategy = GenerationType.SEQUENCE)
private long id;
@ManyToOne
private E_Cart cart;
private String version;
}
If I am trying to save E_CartItem,
entityManager.getTransaction().begin();
E_Cart cart = new E_Cart();
entityManager.persist(cart);
E_CartItem ci = new E_CartItem();
ci.setVersion("one");
ci.setCart(cart);
entityManager.persist(ci);
entityManager.getTransaction().commit();
I am getting below exception:
java.lang.IncompatibleClassChangeError: Class org.hibernate.collection.internal.PersistentMap does not implement the requested interface java.util.Collection
at com.org.entity.E_CartItem.$$_hibernate_write_cart(E_CartItem.java)
at com.org.entity.E_CartItem.setCart(E_CartItem.java:46)
at org.hibernate.bugs.JPAUnitTestCase.hhh1Test(JPAUnitTestCase.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
Questions:
- Am I doing it in correct way?
- I can see the exception is pointing to method $$_hibernate_write_cart in the class file generated by the enhancer.
I have created a branch here, with sample test case.
Requesting any one to suggest what changes are needed so that entity could be saved?