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 CodeCode:
@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();
}