Announcement Announcement Module
Collapse
No announcement yet.
flush() on rollback fix seems to break EntityExistsException Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • flush() on rollback fix seems to break EntityExistsException

    Hi,

    I've been looking around trying to find out what's going on with a test case I have for ensuring that I cannot insert duplicates, where I have a unique constraint AND a generated unique Id.

    I'm using JPA (config is a copy of what Roo generates for JPA), with Hibernate as my persistence provider.

    The relevant parts of my entity bean are:
    Code:
    @Configurable
    @Entity
    public class MyEntity {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long _id;
    
        @Column(unique=true)
        public String resourceId;
    
    }
    And my test is:
    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = { "classpath:/applicationContext.xml" })
    public class MyEntityTest {
    
        @Test
        @Transactional
        public void testPersistDuplicatesFails() {
            MyEntity obj = new MyEntity("resource-test1");
            obj.persist();
    
            MyEntity obj2 = new MyEntity("resource-test1");
            try {
                obj2.persist();
                fail(); // should throw EntityExistsException
            } catch (EntityExistsException e) {
                e.printStackTrace(System.err);
            }
        }
    }
    My test runs correctly, but as part of Spring's transaction support, an attempt is made to flush the context before rolling back. (a recent change to fix another issue, I see from http://jira.springsource.org/browse/SPR-5315).

    The problem is that obj2 is being flushed, which fails, as it was not successfully persisted, and therefore doesn't have it's @Id field populated.

    Is there something that I can/should be doing to prevent this from happening?

    Cheers,

    Neale

    Relevant stack trace is:
    Code:
    org.hibernate.AssertionFailure: null id in MyEntity entry (don't flush the Session after an exception occurs)
    	at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:55)
    	at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:164)
    	at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:120)
    	at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:196)
    	at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
    	at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
    	at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
    	at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:304)
    	at org.springframework.orm.jpa.JpaTransactionManager$JpaTransactionObject.flush(JpaTransactionManager.java:610)
    	at org.springframework.transaction.support.DefaultTransactionStatus.flush(DefaultTransactionStatus.java:165)
    	at org.springframework.test.context.transaction.TransactionalTestExecutionListener$TransactionContext.endTransaction(TransactionalTestExecutionListener.java:501)
    Last edited by nealeu; Jun 11th, 2009, 04:05 AM. Reason: Add [code] tags

  • #2
    No help?

    Okay. Guess it's a bug, so JIRA it is.

    Comment

    Working...
    X