autocommit and @Transactional and Cascading with spring, jpa and hibernate
Posted
by subes
on Stack Overflow
See other posts from Stack Overflow
or by subes
Published on 2009-12-02T18:09:18Z
Indexed on
2010/05/23
16:00 UTC
Read the original article
Hit count: 390
Hi,
what I would like to accomplish is the following:
- have autocommit enabled so per default all queries get commited
- if there is a @Transactional on a method, it overrides the autocommit and encloses all queries into a single transaction, thus overriding the autocommit
- if there is a @Transactional method that calls other @Transactional annotated methods, the outer most annotation should override the inner annotaions and create a larger transaction, thus annotations also override eachother
I am currently still learning about spring-orm and couldn't find documentation about this and don't have a test project for this yet.
So my questions are:
- What is the default behaviour of transactions in spring?
- If the default differs from my requirement, is there a way to configure my desired behaviour?
- Or is there a totally different best practice for transactions?
--EDIT--
I have the following test-setup:
@javax.persistence.Entity
public class Entity {
@Id
@GeneratedValue
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Repository
public class Dao {
@PersistenceContext
private EntityManager em;
public void insert(Entity ent) {
em.persist(ent);
}
@SuppressWarnings("unchecked")
public List<Entity> selectAll() {
List<Entity> ents = em.createQuery("select e from " + Entity.class.getName() + " e").getResultList();
return ents;
}
}
If I have it like this, even with autocommit enabled in hibernate, the insert method does nothing. I have to add @Transactional to the insert or the method calling insert for it to work...
Is there a way to make @Transactional completely optional?
© Stack Overflow or respective owner