Java Reference
In-Depth Information
any configuration (as you'll see later on), we could create a new hibernate.properties
file in the test directories, without interfering with the artifacts used by the production
code. Notice also that we set the property hibernate.hbm2ddl.auto to update , so the
test cases don't need to create the database tables (Hibernate will automatically create
or update the database schema as needed).
Listing 18.7
JPA configuration (persistence.xml)
<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 persistence_1_0.xsd"
version="1.0">
<persistence-unit name="chapter-18">
<!-- properties are loaded from a separate file, and classes are
scanned through annotations -->
</persistence-unit>
</persistence>
Listing 18.8
Hibernate-specific configuration (hibernate.properties)
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.dialect=org.hibernate.dialect.HSQLDialect
hibernate.connection.driver_class=org.hsqldb.jdbcDriver
hibernate.connection.url=jdbc:hsqldb:mem:my-project-test;shutdown=true
As a final note, some frameworks—such as Spring and Unitils (which we cover in the
next chapter)—already provide a similar setup. So even though this infrastructure is
based on real projects (it was not created for the topic samples), provides flexibility
(and can be adapted to your project needs), and is simple enough (just a few classes),
you might prefer—or it might be more suitable for your project—to use such frame-
works instead.
Now that the infrastructure is set, let's move on to our tests, starting with the JPA
entity mapping tests.
18.4
Testing JPA entities mapping
The first step in JPA development is mapping your objects to tables. Although JPA does
a good job of providing default values for most mappings, it's still necessary to tune it
up, typically through the use of Java annotations. And as you annotate the persistent
classes, you want to be sure they're correctly mapped, so you write test cases that exer-
cise the mapping alone (without worrying about how your final code is going to call
the JPA API ).
You might be wondering whether these tests are necessary. What could be wrong?
Unfortunately, despite the fact that JPA is a powerful and helpful tool, many
things could be wrong in the mappings, and the sooner you figure it out, the better,
for instance:
 
 
 
 
 
 
 
Search WWH ::




Custom Search