Note that there are some explanatory texts on larger screens.

plurals
  1. PODeclarative transactions (@Transactional) doesn't work with @Repository in Spring
    text
    copied!<p>I'm trying to make simple application using Spring, JPA and embedded H2 database. Recently I've come across this strange issue with declarative transactions. They just doesn't commit if I autowire my DAO with @Repository annotation. More specifically I get exception on flush:</p> <pre><code>javax.persistence.TransactionRequiredException: Exception Description: No transaction is currently active </code></pre> <p>Here is my setup:</p> <h2>persistence.xml</h2> <pre><code>&lt;persistence-unit name="schedulePU" transaction-type="RESOURCE_LOCAL"&gt; &lt;provider&gt;org.eclipse.persistence.jpa.PersistenceProvider&lt;/provider&gt; &lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt; &lt;properties&gt; &lt;property name="javax.persistence.jdbc.driver" value="org.h2.Driver" /&gt; &lt;property name="javax.persistence.jdbc.url" value="jdbc:h2:~/scheduleDB" /&gt; &lt;property name="javax.persistence.jdbc.user" value="sa" /&gt; &lt;property name="javax.persistence.jdbc.password" value="" /&gt; &lt;property name="eclipselink.target-database" value="org.eclipse.persistence.platform.database.H2Platform" /&gt; &lt;property name="eclipselink.ddl-generation" value="drop-and-create-tables" /&gt; &lt;property name="eclipselink.logging.level" value="FINE"/&gt; &lt;/properties&gt; &lt;/persistence-unit&gt; </code></pre> <h2>Entity</h2> <pre><code>@Entity @Table(name = "Professors") public class Professor { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private String name; public Professor() { } public Professor(String name) { this.name = name; } } </code></pre> <h2>DAO</h2> <pre><code>@Repository public class JpaDao { @PersistenceContext private EntityManager em; @Transactional public void addProfessor(Professor professor) { em.persist(professor); em.flush(); } } </code></pre> <h2>database.xml (included from root spring context)</h2> <pre><code>&lt;beans&gt; &lt;context:component-scan base-package="com.spybot.schedule.dao" /&gt; &lt;bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /&gt; &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"&gt; &lt;property name="persistenceUnitName" value="schedulePU" /&gt; &lt;/bean&gt; &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt; &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt; &lt;/bean&gt; &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt; &lt;/beans&gt; </code></pre> <h2>Controller</h2> <pre><code>@Controller public class HomeController { @Inject JpaDao dao; @RequestMapping("/add") public @ResponseBody String add(String name) { Professor p = new Professor(name); dao.addProfessor(p); return ":)"; } } </code></pre> <p>And now the interesting part. If I remove @Repository annotation from DAO and specify it explicitly in database.xml, everything works fine.</p> <h2>Update</h2> <p>Putting another <code>&lt;tx:annotation-driven /&gt;</code> into <strong>spring servlet</strong> config fixes the problem, but why?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload