hibernate没有自动更新@ElementCollection

时间:2017-03-07 12:44:42

标签: java hibernate jpa

根据本书,当我更新ElementCOllection列表时,我不做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");时 数据库没有自动刷新新邮件,我想知道为什么????

1 个答案:

答案 0 :(得分:0)

不确定您指的是哪本书,但在您的情况下关于@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);