Configuration of your persistence unit
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> <persistence-unit name="TestDataModel" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <!-- list of all jpa entity classes --> <class>de.akquinet.model.MyEntity</class> <properties> <property name="hibernate.hbm2ddl.auto" value="create-drop" /> <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" /> <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" /> <property name="hibernate.connection.url" value="jdbc:hsqldb:." /> <property name="hibernate.jdbc.batch_size" value="0" /> <property name="hibernate.cache.use_second_level_cache" value="true" /> <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" /> </properties> </persistence-unit> </persistence>
Configuration of the jdbc url
If you use the in memory database util and you will not use the default configured jdbc url jdbc:hsqldb:., than place a file named needle.properties in the root directory of the classpath.
For example: needle.properties:
jdbc.url=jdbc:hsqldb:mem:memoryDB
Exampel JPA Entity:
@Entity public class Person { @Id @GeneratedValue(strategy = GenerationType.AUTO) private final Long id = null; @NotNull @Column(nullable = false) private String firstname; @NotNull @Column(nullable = false) private String lastname; ... some getter and setter methods }
JUnit Test:
public class PersonDBTest { private InMemoryDatabaseUtil databaseUtil; @Before public void setUp(){ databaseUtil = new InMemoryDatabaseUtil(); } @Test public void testPersist() throws Exception{ Person person = new Person(); person.setFirstname("firstname"); person.setLastname("lastname"); databaseUtil.saveObject(person); List<Person> all = databaseUtil.loadAllObjects(Person.class); assertEquals(1, all.size()); } @After public void tearDown(){ databaseUtil.deleteAll(); } }