gpt4 book ai didi

java - Hibernate @ManyToOne 不更新表

转载 作者:行者123 更新时间:2023-12-05 07:35:33 28 4
gpt4 key购买 nike

我有 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 references an unsaved transient instance - save the transient instance beforeQuery flushing: com.spring.model.Role at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:144) at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155) at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:162) at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1434)

我在数据库中的表未使用 CASCADE UPDATE 或 INSERT 设置。我找不到合适的解决方案。感谢您的帮助

编辑:

这就是我更新用户的方式

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

编辑2:

我的 hibernate 配置

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

最佳答案

应该使用OneToMany注解的cascade属性,它与数据库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;

关于java - Hibernate @ManyToOne 不更新表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49503238/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com