'Spring Boot Hibernate session beginTransaction() not working

I use Spring boot and Hibernate run some insert/delete native query in my code

I want controll commit and rollback by myself

I don't want to use @Transactional annotation

So I get session and run beginTransaction()

But I always get this Exception

TransactionRequiredException Executing an update/delete query

Code like

 LocalContainerEntityManagerFactoryBean entityManager =this.builder
        .dataSource(primaryDataSource())
        .packages("com...")
        .properties(getProperties())
        .build();


    Session session = entityManager.unwrap(Session.class);;
    Transaction tx = session.beginTransaction();
    session.createNativeQuery(" insert into temp ()....").executeUpdate();
    tx.commit();
    session.flush();
    session.clear();

Which step did I go wrong?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source