title: Spring Write operations are not allowed in read-only mode
body: <p>I know there are a lot of question on SO about this already, but most of them refer to XML configurations, I'm using annotations. My IDE is IntelliJ and I'm using Gradle as my build system.</p>
  
  <p>So I have a really simple web service, I want to enable login and register functionality. My DAO looks like this:</p>
  
  <pre><code>public class UserDAO implements IUserDAO {
  
      @Autowired
      private HibernateTemplate hibernateTemplate;
  
      @Override
      public void addUser(User user) {
          hibernateTemplate.save(user);
      }
  
      @Override
      public User findByUsername(String username) {
          return hibernateTemplate.execute(session -&gt; (User)
                  session.createCriteria(User.class)
                          .add(Restrictions.eq("username", username))
                          .uniqueResult());
      }
  }
  </code></pre>
  
  <p>And my config is this:</p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  public class AppConfig {
  
      @Bean
      public IUserDAO userDAO() {
          return new UserDAO();
      }
  
      @Bean
      public HibernateTemplate hibernateTemplate() {
          return new HibernateTemplate(sessionFactory());
      }
  
      @Bean
      public SessionFactory sessionFactory() {
          return new LocalSessionFactoryBuilder(getDataSource())
                  .addAnnotatedClasses(User.class)
                  .buildSessionFactory();
      }
  
      @Bean
      public DataSource getDataSource() {
          BasicDataSource dataSource = new BasicDataSource();
          dataSource.setDriverClassName("com.mysql.jdbc.Driver");
          dataSource.setUrl("jdbc:mysql://example.com:3306/myDatabase");
          dataSource.setUsername("myUser");
          dataSource.setPassword("myUser123");
  
          return dataSource;
      }
  
      @Bean
      public HibernateTransactionManager hibTransMan() {
          return new HibernateTransactionManager(sessionFactory());
      }
  }
  </code></pre>
  
  <p>I use this in my <code>RestController</code> to get the DAO:</p>
  
  <pre><code>@Autowired
  private IUserDAO userDAO;
  </code></pre>
  
  <p>Executing <code>findByUsername</code> works ok and returns the <code>User</code> that matches the given username. However, the <code>addUser</code> method doesnt work, and gives me this error:</p>
  
  <pre><code>Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
  </code></pre>
  
  <p>I've looked around and found that I should add <code>@Transactional(readOnly = false)</code> to my function, so I did that (I added it above my <code>addUser</code> function, if that's even the right place to put it...), but then I get this error:</p>
  
  <pre><code>No qualifying bean of type [org.springframework.transaction.PlatformTransactionManager] is defined: expected single matching bean but found 2: hibTransMan,transactionManager
  </code></pre>
  
  <p>Can anyone help me with this situation? Also, for any IntelliJ users, the <code>@Autowired</code> annotation in my UserDAO is flagged with a warning that says <code>Autowired members must be defined in the valid spring bean(@Component/@Service,etc.)</code>. Anyone know why this is happening?</p>
  
  <p>P.S. If anybody is startled by the Lambda expression in my <code>findByUsername</code> method, I'm using Java 8.</p>