本文介绍了在只读模式下不允许弹出写操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我知道关于这个问题有很多问题,但其中大多数是指XML配置,我正在使用注释。我的IDE是IntelliJ,我使用Gradle作为构建系统。

因此,我有一个非常简单的Web服务,我希望启用登录和注册功能。我的DAO如下所示:

  public class UserDAO implements IUserDAO {

@Autowired
私人HibernateTemplate hibernateTemplate;

@Override
public void addUser(用户用户){
hibernateTemplate.save(user);

$ b @Override
public User findByUsername(String username){
return hibernateTemplate.execute(session - >(User)
session.createCriteria (User.class)
.add(Restrictions.eq(username,username))
.uniqueResult());
}
}

我的配置是这样的:

  @Configuration 
@EnableTransactionManagement
公共类AppConfig {

@Bean
public IUserDAO userDAO(){
返回新的UserDAO();


$Be
public HibernateTemplate hibernateTemplate(){
return new HibernateTemplate(sessionFactory());



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);

返回dataSource;


$Be
public HibernateTransactionManager hibTransMan(){
return new HibernateTransactionManager(sessionFactory());




$ b我在 RestController 来获取DAO:

  @Autowired 
private IUserDAO userDAO;

执行 findByUsername 正常工作并返回用户,它与给定的用户名匹配。然而, addUser 方法不起作用,并给我这个错误:

 在只读模式下不允许写操作(FlushMode.MANUAL):将会话变为FlushMode.COMMIT / AUTO或从事务定义中移除只读标记。 

我环顾四周,发现我应该添加 @Transactional(readOnly = false)给我的函数,所以我这样做了(我把它添加到我的 addUser 函数上面,如果这是放在正确位置的话。 ..),但后来我得到这个错误:

 没有[org.springframework.transaction.PlatformTransactionManager]类型的限定bean是定义:期望的单个匹配bean,但找到2:hibTransMan,transactionManager 

任何人都可以帮助我解决这种情况吗?另外,对于任何IntelliJ用户,我的UserDAO中的 @Autowired 注释会标记为的警告,Autowired成员必须在有效的spring bean中定义(@组件/ @服务等)。任何人都知道为什么会发生这种情况?



如果有人被我的 findByUsername 方法中的Lambda表达式吓了一跳,那么我使用的是Java 8。

解决我找到了解决方案,我添加了 @Transactional(readOnly = false)注释到我的 addUser 方法和 @Primary 添加到 AppConfig中的 hibTransMan 方法 class。然而,关于 @Autowired 的警告仍然存在,任何人都知道为什么?


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.

So I have a really simple web service, I want to enable login and register functionality. My DAO looks like this:

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 -> (User)
                session.createCriteria(User.class)
                        .add(Restrictions.eq("username", username))
                        .uniqueResult());
    }
}

And my config is this:

@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());
    }
}

I use this in my RestController to get the DAO:

@Autowired
private IUserDAO userDAO;

Executing findByUsername works ok and returns the User that matches the given username. However, the addUser method doesnt work, and gives me this error:

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.

I've looked around and found that I should add @Transactional(readOnly = false) to my function, so I did that (I added it above my addUser function, if that's even the right place to put it...), but then I get this error:

No qualifying bean of type [org.springframework.transaction.PlatformTransactionManager] is defined: expected single matching bean but found 2: hibTransMan,transactionManager

Can anyone help me with this situation? Also, for any IntelliJ users, the @Autowired annotation in my UserDAO is flagged with a warning that says Autowired members must be defined in the valid spring bean(@Component/@Service,etc.). Anyone know why this is happening?

P.S. If anybody is startled by the Lambda expression in my findByUsername method, I'm using Java 8.

解决方案

I found the solution, I added the @Transactional(readOnly = false) annotation to my addUser method, and @Primary to my hibTransMan method in the AppConfig class. However, the warning about @Autowired is still there, anyone know why?

这篇关于在只读模式下不允许弹出写操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-09 01:10