TRIMM JPA Extensions

NOTE: All JPA listeners/extensions are placed in package dk.tigerteam.trimm.mdsd.jpa unless otherwise noted. All Jpa provider specific listeners/extension are in provider specified packages like dk.tigerteam.trimm.mdsd.jpa.hibernate.

EventListener Class Name (FQCN) Description Example
JpaNamedTablesAndColumnsListener Handles everything related to Column, JoinColumn, JoinTable, Table, etc. naming. Click here to go to the sample below
Jpa2OrphanRemovalListener JPA 2.0 specific, only supports owning OneToOne unidirectional or owning OneToMany unidirectional (and not self-referencing) associations to entities which have no relationships to other entities, or which have owning  unidirectional OneToOne or ManyToOneassociations to other entities.
HibernateAssociationUnproxyListener Listener which will allow ManyToOne and OneToOne property GetterMethod’s to perform Unproxification of Hibernate proxified instance variables. And setting Jpa provider specific unproxying Collection wrappers on ManyToMany see UnproxyingWrappedSet for example.
HibernateEnversListener Event listener to handle history/audit of artifacts that require revision history with Hibernate Envers. Supports most features of Envers – see Hibernate specific Stereotypes and Tagged values for the Envers specific Stereotypes (History/Audit and NoHistory/NoAudit)
HibernateDeleteOrphanListener Only supports owning OneToMany unidirectional (and not self-referencing) associations to entities which have no relationships to other entities, or which have owning unidirectional OneToOne or ManyToOne associations to other entities. This functionality is now supported in Jpa version 2 use Jpa2OrphanRemovalListener instead.
HibernateFetchOptimizationListener Supports the following optimizations on associations:
BatchSize: Can be used on class and collection associations: use BATCHSIZE – e.g. 10 value of the batchsizeIf one proxy of a User must be initialized, go ahead and initialize several in the same SELECT.

In other words, if you already know that there are three Item instances in the persistence context, and that they all have a proxy applied to their seller association,you may as well initialize all the proxies instead of just one.Batch fetching is often called a blind-guess optimization, because you don’t know how many uninitialized User proxies may be in a particular persistence context.

Prefetching proxies and collections with a batch strategy is really a blind guess. It’s a smart optimization that can significantly reduce the number of SQL statements that are otherwise necessary to initialize all the objects you’re working with. The only downside of prefetching is, of course, that you may prefetch data you won’t need in the end.

Subselect: can be used on collections SUBSELECT – SUBSELECT

Prefetching using a subselect is a powerful optimization. Subselect fetching is, at the time of writing, available only for collections, not for entity proxies. Also note that the original query that is rerun as a subselect is only remembered by Hibernate for a particular Session.

LazyCollection extra: use EXTRA – EXTRA

To be used in conjunction with Fetch lazyloading on collections for OneToMany and ManyToMany associations.

LazyCollectionOption can be TRUE (the collection is lazy and will be loaded when its state is accessed), EXTRA (the collection is lazy and all operations will try to avoid the collection loading,this is especially useful for huge collections when loading all the elements is not necessary) and FALSE (association not lazy)

Note: If the collection is a Map or a List, the operations containsKey() and get() query the database directly.

HibernateForeignKeyConstraintsListener Listener that can create  named  ForeignKey’s.Note: Hibernate doesn’t support named foreignkeys for AssociationOverrides (which is basically Embeddable entities with associations to real Entities).
HibernateForeignKeyIndexListener More to come
HibernateIndexingListener Applies the Hibernate specific Table annotation to specify Indexes for each class carrying the Table annotation and which has properties with the #INDEX tagged value. Applies “uniqueConstraints” annotation attribute to all classes with the Table annotation and which has properties with the tagged value #UNIQUE_INDEX.

The value of the TaggedValue must adhere to the following pattern: Name[,Order in the Index]. (Note: Name isn’t supported by Hibernate for Unique indexes, but must still be applied in the tagged value)

The optional “Order in the Index” tells in which order the columns should be listed, when defining the index (the order is 1based).
If there are more than one property with the same index name, you MUST apply “Order in the Index”.

Value Examples:


Note: This listener must run after JpaNamedTablesAndColumnsListener has run, otherwise it can’t fill in Column names for the index!

Note: This listener will reuse an existing Hibernate @Table annotation if it already exists.

Note: Only support indexes on simple properties (not on associations and not on embedded properties)

HibernateSortListener  Listener that adds Sort and SortType for sorted collections of embeddable and simple properties.
HibernateValidatorNotNullListener Support for Hibernate Validator NotNull annotation (Now jsr-303).

Supports NotNull (specified using Stereotypes in order to avoid the Hibernate bug specified at and by specifically applying NotNull to required xToOne properties in subclasses where the InheritanceType doesn’t support making properties required. In order to use this {NotNul} feature, you have to make sure that the NotNull‘s AREN’T transformed to the SQL Schema.

This is done by running Hibernate with the following setting:

Please check for details

JpaNamedTablesAndColumnsListener Example

public static class DefaultJpaNamedTablesAndColumnsNameResolver implements JpaNamedTablesAndColumnsNameResolver {

public String resolveJoinColumnName(PropertyEvent event, String defaultJoinColumnName) {
 return defaultJoinColumnName;

public String resolveColumnName(PropertyEvent event, String defaultColumnName) {
 return defaultColumnName;

public String resolveTableName(ClazzEvent event, String defaultTableName) {
 return defaultTableName;

public String resolveJoinTableName(PropertyEvent event, String defaultJoinTableName) {
 return defaultJoinTableName;

public String resolveInverseJoinColumnName(AssociationEvent event, String defaultInverseJoinColumnName) {
 return defaultInverseJoinColumnName;

public String resolveAttributeOverrideColumnName(PropertyEvent event, Property overriddeAttributeProperty,
                                                 String overrideAttributeName, String defaultAttributeOverrideColumnName) {
 return defaultAttributeOverrideColumnName;

public String resolveIndexColumnName(PropertyEvent event, String defaultIndexColumnName) {
 return defaultIndexColumnName;