java – hibernate没有自动更新@ElementCollection
发布时间:2020-12-15 02:18:32 所属栏目:Java 来源:网络整理
导读:根据书中我更新一个ElementCOllection列表,我不做Transection.begin,hibernate将自动提交,但我对它进行了测试,结果出错了 我的Main.java是 public class Main {private static UserService userService;private static Logger logger = LoggerFactory.getLog
根据书中我更新一个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”); 解决方法
不确定您指的是哪本书,但在您的情况下关于@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 为了使更改持久化,您需要将user1实体合并回持久性上下文: userService.merge(user1); 在里面你会打电话: entityManager.merge(user); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |