-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Rollback detaches all?
PostPosted: Tue Dec 12, 2006 2:33 am 
Newbie

Joined: Mon Sep 15, 2003 9:09 am
Posts: 9
I'm using hibernate 3.2.1 GA (core, entitymanager, annotations) standalone.

Is it expected behavior for all instances attached to an EM to become detached after a rollback occurs? If I remove the try/catch/finally block from the test case code below, the code works fine. With that block in place however, I get an exception:

detailMessage "Removing a detached instance com.twocoast.tcsc.hibtest.AlertType#10022"

when em.remove(alertType) is called. If this isn't expected behavior, am I doing something wrong/stupid here? Thanks for any input...

Entity
Code:
@Entity
@Table(name="ALERT_TYPE")
@SequenceGenerator(name="SEQ", sequenceName="ALERT_TYPE_SEQ", allocationSize=1)
public class AlertType {
  private Long id;
  private Integer version;
  private String name;

  public void setId(Long id) {
    this.id = id;
  }
 
  @Id
  @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ")
  @Column(updatable = false, insertable = false)
  public Long getId() {
    return id;
  }
 
  public void setVersion(Integer version) {
    this.version = version;
  }

  @Version
  public Integer getVersion() {
    return version;
  }
 
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

}

Test Case Code
Code:
@Test
public void createAlertType() throws Exception {
    em.getTransaction().begin();
    AlertType alertType = new AlertType();
    alertType.setName("SINGLE_CONNECTION_FAILURE");
    alertType.setMessage("Single database connection failure");
    em.persist(alertType);
    em.getTransaction().commit();
   
    try {
      em.getTransaction().begin();
      AlertType a = new AlertType();
      a.setName("SINGLE_CONNECTION_FAILURE");
      em.persist(a);
      em.flush();
      fail("Unique contraint on name not enforced");
    } catch (EntityExistsException e) {
      ;
    } finally {
      em.getTransaction().rollback();
    }
   
    em.getTransaction().begin();
    em.remove(alertType);
    em.getTransaction().commit();
  }


Top
 Profile  
 
 Post subject: Got it.
PostPosted: Tue Dec 12, 2006 1:24 pm 
Newbie

Joined: Mon Sep 15, 2003 9:09 am
Posts: 9
Reading the new hibernate book on the train this morning clued me in. Very well written, by the way. Just wish I'd read the relevant part sooner.

Whenever an exception occurs (or even just a rollback), you have to throw away the Session (or EntityManager) in which it occurred. All attached objects are detached. Please correct me if I've got that wrong...


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 17, 2006 5:00 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Yes, the spec mandate to detach objects when a rollback() is called.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.