3

The problem I got

After adding a additional Test, which should use a @ActiveProfiles, i got exceptions which are rise when the database-schema.sql script is running twice on the same database.

What framework i use

  • spring:3.2.13.Release & spring-data-jpa:1.6.5.RELEASE & hsqldb:2.3.2 & hibernate-entitymanager:4.3.1.FINAL
  • junit:4.12 & spring-test:3.2.13.RELEASE

What do i use in my code/tests

  • to create the datasource i use <jdbc:embedded-database id="dataSource"> in my .../test/resources/spring/testConfig.xml
  • @RunWith(SpringJUnit4ClassRunner.class) and @ContextConfiguration(locations = {"classpath:spring/testConfig.xml"}) for both of my test
  • the test with @ActiveProfiles extended the other test <- i checked, if this is the problem here, but it isnt.

My Guess

after adding a @ActiveProfiles the spring contest has to be recreated because there are other beans or other settings which depends on the active profile. But it seems that the database isnt dropped.

What i dont want

I dont want to change the schema.sql file to something like

create table exampleModel if not exists 

because this script is generated automaticly in intervals, and in my production situation there are many custom types and tables, which i dont want to edit manualy.

What i want

reuse of the database (so its not initialized for every test)

Way to reproduce

  1. Checkout my github-repo

  2. run mvn test in ./test/

Exception

java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [spring/testConfig.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Invocation of init method failed; nested exception is org.springframework.dao.DataAccessResourceFailureException: Failed to execute database script; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement at line 1 of resource class path resource [testDB/schema.sql]: CREATE TYPE TEXT AS VARCHAR(1000000)
...
Caused by: java.sql.SQLSyntaxErrorException: object name already exists: TEXT in statement [CREATE TYPE TEXT AS VARCHAR(1000000)]
...
Caused by: org.hsqldb.HsqlException: object name already exists: TEXT
Community
  • 1
  • 1
r-vanooyen
  • 101
  • 1
  • 8

2 Answers2

0

For reusing your database schema, you can change your hibernate.hbm2ddl.auto (in testConfig.xml) value to for example update. See: this

  • i got the same Exception when changing the `hibernate.hbm2ddl.auto` to `update` – r-vanooyen Nov 29 '18 at 12:41
  • @ooyen Is Urosh's comment worked? If not maybe there are some configuration issues in Spring configs. Like duplication of database connection configuration or model configurations. Also you can check case sensitivity of both spring and database. i had received this error before and the problem was case sensitivity. – Mehmet Özdemir Nov 29 '18 at 12:52
  • as you can see in my github repo the entities has @Table annotations with the exact equal name as the table itself. the comment of Urosh didnt worked – r-vanooyen Nov 29 '18 at 13:15
  • 1
    @ooyen I was talking about your database table names. Sometimes spring creates table with different names for example you write `@Table(name="EXAMPLE")` but it creates with `example`. Which is causing by case sensitivity. Also you can try `hibernate.hbm2ddl.auto` with `create-drop`. – Mehmet Özdemir Nov 29 '18 at 14:49
  • that didnt work either because then it will drop all of my testcontent, which will be in my schema.sql file too. – r-vanooyen Nov 30 '18 at 13:19
0

Modify your jdbc:embedded-database tag as follows:

<jdbc:embedded-database id="dataSource" generate-name="true">

See Spring doc related to embedded database support for more details.

Pino
  • 7,468
  • 6
  • 50
  • 69