Hibernate @ManyToOne不更新表

时间:2018-03-27 01:54:35

标签: java hibernate

我有2个实体 - 用户和角色@ManyToOne。我想更改用户角色,但角色也希望更新。

用户实体:

@ManyToOne
@JoinColumn(name = "role_id", insertable =  false, updatable = false)
private Role role;

角色实体:

@OneToMany(mappedBy = "role")
private Set<User> users;

我得到的错误是:

  

java.lang.IllegalStateException:org.hibernate.TransientObjectException:object引用未保存的瞬态实例 - 在查询刷新之前保存瞬态实例:com.spring.model.Role           在org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:144)           在org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155)           在org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:162)           在org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1434)

我在DB中的表格未使用CASCADE UPDATE或INSERT设置。我无法找到合适的解决方案。谢谢你的帮助

编辑:

这是我更新用户的方式

public void update(User user) {
        User entity = dao.findById(user.getId());
        if(entity!=null) {
            entity.setRole(user.getRole());
        }
    }

EDIT2:

我的休眠配置

@Configuration
@EnableTransactionManagement
@ComponentScan({ "com.spring.configuration" })
@PropertySource(value = { "classpath:application.properties" })
public class HibernateConfiguration {

    @Autowired
    private Environment environment;

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan(new String[] { "com.spring.model" });
        sessionFactory.setHibernateProperties(hibernateProperties());
        return sessionFactory;
     }

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
        dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
        dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
        dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
        return dataSource;
    }

    private Properties hibernateProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
        properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
        properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
        return properties;        
    }

    @Bean
    @Autowired
    public HibernateTransactionManager transactionManager(SessionFactory s) {
       HibernateTransactionManager txManager = new HibernateTransactionManager();
       txManager.setSessionFactory(s);
       return txManager;
    }
}

1 个答案:

答案 0 :(得分:0)

您应该使用cascade注释的OneToMany属性,它与数据库cascade操作没有任何关系(但可能会受到影响)。

@OneToMany(cascade = CascadeType.ALL, mappedBy="role", orphanRemoval = true)
private Set<User> users;

这样,操作将传播到集合元素。您可以查看this answer了解详情。

您还可以修改ManyToOne注释的级联属性。

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "role_id")
private Role role;