title: JPA without transaction
body: <p>I´m new to JPA. I´m developing an application which uses JPA (Hibernate implementation) and Spring. I´ve declared a persistence unit in my persistence.xml and configuration about EntityManagerFactory in my Spring config files. Something like this:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;bean id="myEmf"   class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
    &lt;property name="dataSource" ref="dataSource" /&gt;
    &lt;property name="packagesToScan" value="my.package" /&gt;
    &lt;property name="jpaVendorAdapter"&gt;
       &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /&gt;
    &lt;/property&gt;
    &lt;property name="jpaProperties"&gt;
       &lt;props&gt;
          &lt;prop key="hibernate.hbm2ddl.auto"&gt;create-drop&lt;/prop&gt;
          &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQL5Dialect&lt;/prop&gt;
       &lt;/props&gt;
    &lt;/property&gt;
  </code></pre>
  
  <p></p>
  
  <p>Then I have some DAOs where I inject the entityManager with the @PersistenceContext annotation:</p>
  
  <pre><code>public MyDaoImpl implements MyDao{
     private EntityManager entityManager;
  
     @PersistenceContext
     private void setEntityManager(EntityManager em){
          this.entityManager = em;
      }
   } 
  </code></pre>
  
  <p>And finally, I have some services where DAOs are injected (by @Autowired Spring's annotation):</p>
  
  <pre><code>public MyServiceImpl implements MyService{
    @Autowired
    private MyDao myDao;
  
    public List&lt;MyEntity&gt; readOperation(){
     //
     return myDAo.searchAll();
   }
  }
  </code></pre>
  
  <p>As its a read only operation I thought it wasn´t needed the @Transactional annotation, but without it, there is an exception:</p>
  
  <pre class="lang-none prettyprint-override"><code>java.lang.IllegalStateException: No transactional EntityManager available
      at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:223)
      at $Proxy121.unwrap(Unknown Source) 
  </code></pre>
  
  <p>I´ve read some other posts like this: <a href="https://stackoverflow.com/questions/14522691/java-lang-illegalstateexception-no-transactional-entitymanager-available][1]">java.lang.IllegalStateException: No transactional EntityManager available</a></p>
  
  <p>And all is said is that the transactional annotation is needed. It´s true that it works with it, but I´d like to know (and why) if all methods (even read only operations) must be transactional.</p>