Controlling the JPA inheritance and id strategy when generating JPA code

Controlling the JPA inheritance and id strategy when generating JPA code

If you don’t provide a hand coded rootMappedSuperClass then you need to specify both a inheritanceStrategy and a idStrategy:

jpaSetup:
 inheritanceStrategy:
  implementation: dk.tigerteam.mdsd.jpa.DefaultJpaInheritanceAndClazzesStrategy
 idStrategy:
  implementation: dk.tigerteam.mdsd.jpa.DefaultJpaIdStrategy

Both the inheritanceStrategy and the idStrategy parameters accept a FQCN (Fully Qualified Class Name) for a class on the generator class path (e.g. specifying a in your Maven pom) or a project relative path for a .groovy script.

For the idStrategy the FQCN or .groovy script needs to contain a class which implements the dk.tigerteam.trimm.mdsd.jpa.JpaIdStrategy. The default provided implementation dk.tigerteam.mdsd.jpa.DefaultJpaIdStrategy will look for an UML property/attribute which has the stereotype ID (case insensitive). This property will be annotated with the @javax.persistence.Id annotation.

For the inheritanceStrategy the FQCN or .groovy script needs to contain a class which implements the dk.tigerteam.trimm.mdsd.jpa.JpaInheritanceAndClazzesStrategy. The default provided implementation dk.tigerteam.trimm.mdsd.jpa.DefaultJpaInheritanceAndClazzesStrategy default to javax.persistence.InheritanceType#SINGLE_TABLE for all class hierarchies (both supports generating JPA annotated code for both the javax.persistence.InheritanceType#SINGLE_TABLE and javax.persistence.InheritanceType#JOINED strategies).

If the default inheritance strategy (which is also the default for the TRIMM JPA YAML configuration based generator dk.tigerteam.trimm.mdsd.jpa.configuration.JpaConfigurationBasedCodeGenerator if you either specify a rootMappedSuperClass parameter or leave out the inheritanceStrategy parameter) doesn’t fit your needs then you can roll your own easily.
We recommend inheriting from the dk.tigerteam.trimm.mdsd.jpa.DefaultJpaInheritanceAndClazzesStrategy as shown in the example below:

import dk.tigerteam.mddexample.model.AbstractEntity
import dk.tigerteam.trimm.mdsd.java.codedom.Clazz
import dk.tigerteam.trimm.mdsd.java.codedom.Type
import dk.tigerteam.trimm.mdsd.jpa.DefaultJpaInheritanceAndClazzesStrategy

import javax.persistence.DiscriminatorType
import javax.persistence.InheritanceType

public class CustomJpaInheritanceStrategy extends DefaultJpaInheritanceAndClazzesStrategy {
 
       @Override
       public InheritanceType getInheritanceType(Clazz clazz) {
             return InheritanceType.JOINED
       }
 
       @Override
       public Type resolveMappedSuperClazzType(Clazz clazz) {
             return new Type(AbstractEntity.class)
       }
 
       @Override
       public String getDiscriminatorColumnName(Clazz clazz) {
             return "CLS_ID"
       }
 
       @Override
       public DiscriminatorType getDiscriminatorValueType(Clazz entityClazz) {
             return DiscriminatorType.STRING;
       }
 
       @Override
       public String getDiscriminatorValue(Clazz clazz) {
            return clazz.getName();
       }
}
 
Comments

No comments yet.