How can I make this work in unit tests using Hibernate 3.3.1ga and HSQLDB:
@Entity
@Table(name="CATEGORY", schema="TEST")
public static class Category { ... }
The problem is that Hibernate expects the schema to exist. The second problem is that Hibernate issues the CREATE TABLE TEST.CATEGORY
before any of my code runs (this happens deep inside Spring's test setup), so I can't get a connection to the DB before Hibernate and create the schema manually.
But I need the schema because I have to access different databases in the real code. What should I do?
Hibernate 3.3.1ga, HSQLDB, Spring 2.5
You could write a class that implements InitializingBean:
You then have to define a bean in your bean definition file of this class (I'm taking a shot in the dark as to what your existing bean definitions look like).
By using the
depends-on
attribute of Hibernate's bean, Spring will ensure that theschemaCreator
bean will be initialized first, causing the schema to exist just in time. This should also make your intentions clearer.My current solution looks like this:
but that feels like a really ugly hack. Isn't there a better solution?
Below is an example of how you can create spring config with test hslqdb It automaticly detects all your schemas from @Table(schema =...) and creates them for you.
If it is just for testing this should work for you:
And here is a test sample:
I ran into the same problem where MS SQL Server wants the catalog and schema defined, but HSQLDB does not. My solution was to load a custom orm.xml file (via persistence.xml) specifically for MS SQL Server that sets the catalog and schema.
1.Only specify the @Table name (omit any catalog or schema info) for your entity:
2.Specify two persistence-unit nodes in your META-INF/persistence.xml file
3.Specify the default catalog and schema in the orm-mssql.xml file:
4.I'm using Spring to configure JPA, so I use a property-placeholder for the value of the persistenceUnitName:
For unit tests, use 'com.mycompany.test' and for integration-tests/production deployments, use 'com.mycompany.prod'.
It looks to me like you have a reproducible bug in the Hibernate DDL creation code. You should report a bug - it's a long term solution but it's the way things are done in open source. Of course you might want to produce a patch, but I never found the Hibernate code base easy to hack.