'Java Hibernate - is it possible to save the result to the database without commit, so that I can use sql / hql to verify data and rollback if needed?

I'm using Java with Hibernate. I want to:

  1. Save my data to the database
  2. Run sql to verify the result
  3. If the result is valid, then commit, otherwise rollback

So, is it possible to save the result to the database without commit, so that I can use sql / hql to verify data and rollback if needed?


My actual scenario is quite complicated, the simplified version is:

  • PERSON joins PERSON_CAR joins CAR joins CAR_SEAT joins SEAT
  • Make changes and commit everything
  • If any PERSON has more than 10 SEATs, I want to show errors

If I could save everything to the database first, then I can write SQL with GROUP BY and HAVING statement to aggregate the data and only return the ones that exceed.



Solution 1:[1]

Yes, there is session.flush() function, which could be used for that purpose.

Sources

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

Source: Stack Overflow

Solution Source
Solution 1