Hibernate did not automatically update @ElementCollection

According to the book, when I update the ElementCOllection list, I do not do Transection.begin, the sleep mode will be automatically fixed, but I did a test on it, the result has something wrong

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

}

and my configuration is here, coding according to 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;
}

}

Essence 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");
in the database there was no automatic dumping of a new message into it, I want to know why ????

+4
source share
1 answer

Not sure which book you have in mind, but the key in @ElementCollection in your case is that all operations are cascaded by default.

, , , , .. .

:

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

Source: https://habr.com/ru/post/1671704/


All Articles