One can use the provided _ClassType_ to map a _java.lang.Class_ to a _VARCHAR_.
This approach has a lot of drawbacks.
* The application specific full classname is saved in the database. When different application wants to access same database problem arises if different code base is used. * Violates separation of concern aswell it is prone to refactorization. When packages or class names are refactored it requires database batch updates which is just unnecessary maintenance work, especially in production. * VARCHAR is used which is harder to index, especially in unique database constraints
My first idea was a converter for mapped entities that converts their class to their real table names. So instead of saving _package.entities.MyFoo_ in database it is converted to _my_foo_ instead. The metadata is available in _ClassMetaData_ of configuration.
For my use case this is sufficient but it is restricted to mapped classes. So why not use a configuration element to define those mappings.
{code:xml} <classMappingType> <class="package.entities.MyFoo" value="my_foo"/> <class="package.entities.MyBar" value="my_bar"/> </classMappingType> {code}
Add targeted SQL type:
{code:xml} <classMappingType> <class="package.entities.MyFoo" value="1" type="long"/> <class="package.entities.MyBar" value="2" type="long"/> </classMappingType> {code}
This approach resolves all mentioned disadvantages of using _ClassType_.
----
It could even be more generic by allowing the definition of arbitrary mapping types in xml that can be referenced as a property type:
{code:xml} <!-- define custom types --> <mappingType name="ClassMappingType"> <key="package.entities.MyFoo" value="1" type="long"/> <key="package.entities.MyBar" value="2" type="long"/> </mappingType>
<!-- Entity mapping - use defined type --> <property name="refType" type="ClassMappingType"/> {code} |
|