2 Replies Latest reply on Jun 28, 2011 7:35 AM by Michal Klimuk

    problem auditing an EnumMap collection

    Michal Klimuk Newbie

      Hello all,

       

      we are using Envers to audit content of our JPA/Hibernate based project. We have been working recently on a feature that required usage of an EnumMap and we came across a hibernate exception while persisting an entity containing the map :

       

      Caused by: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [Product_ProductLocalizedInfo_AUD#{REV=DefaultRevisionEntity(id = 1, revisionDate = 20 juin 2011 13:24:16), localizedInfo_id=2, Product_id=1, mapkey=en}]

                at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:190)

                at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:143)

                at org.hibernate.ejb.event.EJB3SaveEventListener.saveWithGeneratedId(EJB3SaveEventListener.java:62)

                at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)

                at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)

                at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)

                at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)

                at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)

                at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:713)

                at org.hibernate.impl.SessionImpl.save(SessionImpl.java:701)

                at org.hibernate.envers.strategy.DefaultAuditStrategy.performCollectionChange(DefaultAuditStrategy.java:28)

                at org.hibernate.envers.synchronization.work.PersistentCollectionChangeWorkUnit.perform(PersistentCollectionChangeWorkUnit.java:88)

                at org.hibernate.envers.synchronization.AuditProcess.executeInSession(AuditProcess.java:114)

                at org.hibernate.envers.synchronization.AuditProcess.doBeforeTransactionCompletion(AuditProcess.java:152)

                at org.hibernate.engine.ActionQueue$BeforeTransactionCompletionProcessQueue.beforeTransactionCompletion(ActionQueue.java:543)

                at org.hibernate.engine.ActionQueue.beforeTransactionCompletion(ActionQueue.java:216)

                at org.hibernate.impl.SessionImpl.beforeTransactionCompletion(SessionImpl.java:571)

                at org.hibernate.jdbc.JDBCContext.beforeTransactionCompletion(JDBCContext.java:250)

                at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:138)

                at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:76)

                at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:467)

       

      Entity definition looks like below (see the joint project for details).

       

      @Entity

      @Audited

      public class Product {

       

       

                @Id

                @GeneratedValue(strategy = GenerationType.AUTO)

                private long id;

       

       

                private String code;

       

       

                @OneToMany(cascade = CascadeType.ALL)

                private Map<Language, ProductLocalizedInfo> localizedInfo = new EnumMap<Language, ProductLocalizedInfo>(

                                    Language.class);

       

      ...

      }

      We found out that the problem occurs because the EntrySetIterator on our map points always to the same object. When we change map implementation to HashMap everything works fine... We don't know if the behaviour is caused by our configuration or is it a broader issue.

      We would be greatful for any opinions on that.

      I join a maven project that ilustrates the problem.

       

      Thanks in advance !

      Greetings,

      Michal