java – How to solve CommandAcceptanceException in an integration test?

Question:

There is a DAO method that I am testing with hsqldb .

When creating a SessionFactory a CommandAcceptanceException on

SessionFactory fac = new AtomicReference<>(
                new Configuration().configure().buildSessionFactory()
        );

Log:

Hibernate: alter table propose drop constraint FK1whqgtiifg9s1e1rxkuhbgqhk
2017-08-03T23:41:27.194+0300  WARN  GenerationTarget encountered exception accepting command : Error executing DDL via JDBC Statement

Мой hibernate.cfg.xml:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="http://www.hibernate.org/xsd/orm/cfg">
    <session-factory>
        <property name="connection.url">jdbc:hsqldb:mem:auto_mag;sql.enforce_size=false</property>
        <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
        <property name="connection.username">sa</property>
        <property name="connection.password"></property>
        <property name="dialect">org.hibernate.dialect.HSQLDialect</property>
        <property name="show_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">create</property>

        <mapping resource="ru/pravvich/model/User.hbm.xml" />
        <mapping resource="ru/pravvich/model/Propose.hbm.xml" />
    </session-factory>
</hibernate-configuration>

Why is this exception being thrown?

How to remove it?

UPDATE

Here is an example test

public class DAOImplTest {

    private SessionFactory fac;
    private DAO dao;

    private final User user = new User("test", "test");
    private final Propose propose = new Propose(
            user, "mark", "model", "desc", new byte[]{});


    @Before
    public void before() {
        fac = new Configuration().configure().buildSessionFactory();

        dao = new DAOImpl(fac);
    }

    @After
    public void after() {
        fac.close();
    }

    @Test
    public void whenAddNewUserAndGetUserByLoginPasswordThanUserExistInDB() {

        dao.addUser(new User("test", "test"));

        assertNotNull(dao.getUser("test", "test"));
    }
}

Answer:

Hibernate: alter table propose drop constraint FK1whqgtiifg9s1e1rxkuhbgqhk

It looks like a hibernate bug when using the in memory database. Which has been fixed in versions: 4.3.0.Beta4.

Try to put:

<property name="hibernate.hbm2ddl.auto">update</property>

Hibernate tries to drop tables (due to the fact that the create strategy is selected) that were not created because the database is created from scratch every time.


Update:

yes, it removes the error, but with this approach, my tests fail because they are designed for the fact that the database should be recreated.


Use @After annotation. If you have an entityManager, then:

@After
public void after() throws Exception {
    if (entityManager.getTransaction().isActive()) {
        entityManager.getTransaction().rollback();
    }
}

Or via datasource

@After
public void tearDown() {
    try {
        clearDatabase();
    } catch (Exception e) {
        fail(e.getMessage());
    }
}


public void clearDatabase() throws Exception {
  DataSource ds = (DataSource) SpringApplicationContext.getBean("mydataSource");
  Connection connection = null;
  try {
    connection = ds.getConnection();
    try {
      Statement stmt = connection.createStatement();
      try {
        stmt.execute("TRUNCATE SCHEMA PUBLIC RESTART IDENTITY AND COMMIT NO CHECK");
        connection.commit();
      } finally {
        stmt.close();
      }
    } catch (SQLException e) {
        connection.rollback();
        throw new Exception(e);
    }
    } catch (SQLException e) {
        throw new Exception(e);
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
}

Option with SessionFactory

public void clearDatabase() throws Exception {

   try (Session session = fac.openSession()) {
       Query query = session.createQuery("TRUNCATE SCHEMA PUBLIC RESTART IDENTITY AND COMMIT NO CHECK");       

       query.executeUpdate();

   } catch (SQLException e) {
        throw new Exception(e);
   } 
}
Scroll to Top