Java Reference
In-Depth Information
HibernateMappingTests
HibernateMappingTests is the base class for writing tests for the O/R mapping. It
provides methods for making assertions about the O/R mapping, including:
assertClassMapping() : Verifies that the class is mapped to the specified
table
assertAllFieldsMapped() : Verifies that all of the fields of a class are
mapped
assertIdField() : Verifies that the class's id field is mapped to the specified
column
assertField() : Verifies that a field is mapped to the specified columns
HibernateMappingTests call the Hibernate metadata API s to find out about the
O/R mapping. The Hibernate metadata API exposes the O/R mapping as Java
objects. A test obtains the O/R metadata for a class by calling getClassMapping()
on the Configuration object that constructs the SessionFactory .
Configuration cfg = …;
PersistentClass classMapping =
cfg.getClassMapping(PendingOrder.class.getName());
This method takes a Java class as a parameter and returns a PersistentClass
object that describes its O/R mapping. The Hibernate metadata API s are only doc-
umented in the JavaDoc and not the manual, but they appear to be relatively sta-
ble. The only issues with using them is that the tests must call
Configuration.openSessionFactory() in order to ensure that Hibernate com-
pletely initializes the metadata. This can sometimes result in a database connec-
tion being opened, which can slow down the tests slightly.
Here is an excerpt of the source code for HibernateMappingTests that shows
how assertClassMapping() verifies that the class is mapped to the specified table:
public abstract class HibernateMappingTests extends TestCase {
private static Configuration cfg;
private PersistentClass classMapping;
private Class type;
protected void assertClassMapping(Class type, String tableName) {
this.type = type;
classMapping = cfg.getClassMapping(type);
assertEquals(tableName, classMapping.getTable().getName());
}
}
 
 
 
 
 
 
 
 
Search WWH ::




Custom Search