Why are database prohibitions in HSQLDB checked only during commit when using transactions in Hibernate?

I found strange behavior in HSQL, it seems that when using databases, database prohibitions are not checked during SQL inserts, but during SQL transactions and when the transaction is rolled back, they are not checked at all.

I have a Spring integration test:

@RunWith(SpringJUnit4ClassRunner.class)
@TransactionConfiguration(defaultRollback=true, transactionManager="transactionManager")
@Transactional
public class IntegrationTest {

with a test that creates a new instance of the entity and calls Hibernate on it persist.

It works fine, however, when I change defaultRollbackto false, it fails:

Caught exception while allowing TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener@517a2b0] to process 'after' execution for test: method [public void MyIntegrationTest.test()], instance [MyIntegrationTest@546e61d5], exception [null]
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
  at org.springframework.orm.hibernate4.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:161)
  at org.springframework.orm.hibernate4.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:681)
  at org.springframework.orm.hibernate4.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:563)
  at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:757)
  at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:726)
...
Caused by: java.sql.SQLIntegrityConstraintViolationException: integrity constraint violation: NOT NULL check constraint; SYS_CT_10120 table: MYTABLE column: MYCOLUMN

This seems to be correct, because really my code did not set the mycolumn attitribute attribute in the entity before calling persiston it.

Questions:

  • why database prohibitions are not checked during insertions, but during commit?
  • ?
+4
1

[Posting @a_horse_with_no_name , ].

ORM (Hibernate) . , . , flush() (FlushMode.ALWAYS) , persis() .

+1

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


All Articles