gpt4 book ai didi

java - hibernate 没有自动更新@ElementCollection

转载 作者:搜寻专家 更新时间:2023-11-01 03:47:26 26 4
gpt4 key购买 nike

根据书上的说法,当我更新一个 ElementCOllection List 时,我没有做 Transection.begin,hibernate 会自动提交,但我做了一个测试,结果有问题

我的Main.java是

public class Main {

private static UserService userService;

private static Logger logger = LoggerFactory.getLogger(Main.class);


public static void main(String[] args) {
ApplicationContext applicationContext =
new AnnotationConfigApplicationContext(RootConfig.class);
userService = applicationContext.getBean(UserService.class);


User user = new User("qwerty");
user.getMessages().add("hello,world");
userService.save(user);
User user1 = userService.findByName("qwerty");
user1.getMessages().add("ncjdksckds");
System.out.println(user);
}

我的配置在这里,按照书上的编码

@Configuration
@ComponentScan(basePackages = {"org.zhy"})
@EnableJpaRepositories(basePackages = {"org.zhy.repository"})
@EnableTransactionManagement
public class RootConfig {
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter ) {
LocalContainerEntityManagerFactoryBean emfb =
new LocalContainerEntityManagerFactoryBean();
emfb.setDataSource(dataSource);
emfb.setJpaVendorAdapter(jpaVendorAdapter);
emfb.setPersistenceUnitName("demo");
Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
emfb.setJpaProperties(hibernateProperties);

emfb.setPackagesToScan("org.zhy.domain");
return emfb;
}

@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}

@Bean
public DataSource dataSource() {
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
// some setting here such as url...
return ds;
}
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setGenerateDdl(false);
adapter.setDatabase(Database.MYSQL);
adapter.setShowSql(true);
adapter.setDatabasePlatform("org.hibernate.dialect.MySQLDialect");
return adapter;
}

实体在这里

@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@ElementCollection(fetch = FetchType.EAGER)
private List<String> messages = new ArrayList<String>();
//getter and setter

当我使用 user1.getMessages().add("ncjdksckds");
数据库没有自动将新消息刷新到其中,我想知道为什么????

最佳答案

不确定您指的是哪本书,但在您的案例中,关于@ElementCollection 的关键是默认情况下所有操作都是级联的。

假设您的服务的所有公共(public)方法都标记为事务性的,在您查询用户后,它是一个分离的实体..因为它从现在开始不在任何事务范围内。

在您的代码中:

User user = new User("qwerty");
user.getMessages().add("hello,world");
userService.save(user); // new transaction start and finish
User user1 = userService.findByName("qwerty"); // new transaction start and finish
user1.getMessages().add("ncjdksckds"); // this change is outside of a transaction

为了使更改持久化,您需要将 user1 实体merge 返回到持久性上下文中:

userService.merge(user1);

在里面你会调用:

entityManager.merge(user);

关于java - hibernate 没有自动更新@ElementCollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42648681/

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