Hibernate version: hibernate-3.1beta3, hibernate-entitymanager-3.1beta3
Name and version of the database you are using: embedded hsqldb
I'm creating a small sample ejb3 project with 1 entity bean and 1 session bean.
Code:
@Entity
@Table(name="ledger_accounts")
public class LedgerAccount implements Serializable {
private long id;
private String description;
public LedgerAccount() {
}
@Id(generate=GeneratorType.AUTO)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Column(name="description", nullable=false, length=50)
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
Code:
public @Stateless class LedgerServiceBean implements LedgerService {
@PersistenceContext private EntityManager em;
public void persistLedgerAccount(LedgerAccount ledgerAccount) {
em.persist(ledgerAccount);
}
public void setEntityManager(EntityManager em) {
this.em = em;
}
}
If I understand correctly in a CMP environment this should be enough. The entity manager is injected and the container takes care of transaction behaviour.
I'm developing this sample outside a j2ee container and using junit tests to test the functionality. Because the session bean is a pojo I can instatiate it to test the functionality.
I can also create a new entity manager
Code:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("reflection-rsa-em");
EntityManager em = emf.createEntityManager();
and set it the session bean.
Because the container would take care of the transactions I get an exception that no transaction is in progress.
Code:
Exception in thread "main" javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate.ejb.AbstractEntityManagerImpl.checkTransactionActive(AbstractEntityManagerImpl.java:123)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:128)
at reflection.rsa.services.LedgerServiceBean.persistLedgerAccount(LedgerServiceBean.java:32)
at reflection.rsa.test.services.LedgerServiceTester.<init>(LedgerServiceTester.java:39)
at reflection.rsa.test.services.LedgerServiceTester.main(LedgerServiceTester.java:57)
How can I deal best with this in a j2se environment?