Java – hibernate does not automatically update the @ elementcollection

According to the book, I update a list of elementcollection, I don't do transition Begin, hibernate will submit automatically, but I tested it and found an error

My main Java is

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

}

My configuration is here, according to the code of the book

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

}

The entity is here

@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

When I use user1 getMessages(). add(“ncjdksckds”); The database does not automatically print new messages into it. I want to know why????

Solution

I'm not sure which book you mean, but in your case, the key to @ elementcollection is that all operations are cascaded by default

Suppose your service marks all public methods as transactional. After querying users, it is a separate entity Because it is outside the scope of any transaction from now on

In your code:

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

To persist the changes, you need to merge the user1 entity back into the persistence context:

userService.merge(user1);

Inside you will call:

entityManager.merge(user);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>