根据这本书,当我更新ElementCOllection List时,我不执行Transection.begin,休眠将自动提交,但是我对其进行了测试,结果有问题

我的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的关键是默认情况下所有操作都是级联的。

假设您的服务具有所有标记为事务性的公共方法,则在查询用户之后,它是一个分离的实体。从现在开始,它在任何事务范围之外。

在您的代码中:

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

为了使更改持久化,您需要将merge实体user1回到持久化上下文中:
userService.merge(user1);

在内部您会打电话给:
entityManager.merge(user);

08-19 22:38