質問

I'm using hibernate with annotations and hibernate tools with freemarker templates to automatically generate my orm dao classes.

Now I'd like to implement hibernate search and therefor want to add hibernate search annotations to these classes and properties.

But I don't know how and where I can configure which classes and properties I want hibernate tools to annotate.

The problem with freemarker templates is, that i can only define class-generation strategies in general for all classes. But for the hibernate search implementation i want to annotate only specific classes and only specific attributes within these classes.

Is there any way where i can define which classes and getter properties i'd like to have annotated by hibernate tools? Or annotate the classes after the build process automatically?

Solutions I could rule out so far:

  • manually add annotations after the code generation -> it has to be automatically
  • use of several if-statements in the freemarker templates like "if class name == test do add @Index to class" -> it just would be to much, especially with the annotations for the properties
  • add some settings to reveng.xml file -> at least I don't know how this could help

Thanks for your help!

役に立ちましたか?

解決

How to add hibernate search annotations to classes and properties in the DAO generation using Hibernate tool task.

It seems there is no way to tell hibernate to do this task for adding custom annotations.But you can do with some code.

You should extend hibernate POJOExporter class and in the extended class, add all your key&Value in additionalContext.These Key&Values will be available in your ftl file.And in the ftl you can take decision to add annotation based these key&value props. Here is extended pojo exporter

public class JavaDAOExporter extends PojoExporter {

    private static final String JAVA_DAO_FTL = "templates/hibernate/java/dao.ftl";
    private String serviceId;

    public JavaDAOExporter() {
        super();
    }

    public JavaDAOExporter(Configuration cfg, File outputdir, String serviceId, MetaDataDialect metadataDialect) {
        super(cfg, outputdir, metadataDialect);
        this.serviceId = serviceId;
    }

    protected void init() {
        super.init();
        setTemplateName(JAVA_DAO_FTL);
        setFilePattern("{package-name}/dao/{class-name}Dao.java");
    }
    @Override
    protected void exportPersistentClass(Map additionalContext, POJOClass element) {
        //Element is the pojo class,here you can take decision to add annotation in the class,if so add your key&value attributes.Which will be used in ftls.
        additionalContext.put("searchAnnotationForClass", true);
        exportPOJO(additionalContext, element);
    }

    public String getName() {
        return "hbm2javadao";
    }

    protected void exportComponent(Map additionalContext, POJOClass element) {
        // noop - we dont want components
    }

}

And template tooks like

package ${pojo.getPackageName()}.dao;

<#assign classbody>
<#assign declarationName = pojo.importType(pojo.getDeclarationName())>
import org.springframework.stereotype.Repository;

@Repository("${serviceId}.${declarationName}Dao")
//Adding annotation only if searchAnnotationForClass is true
<#if true = searchAnnotationForClass>
@Search("${serviceId})
</#if>
public class ${declarationName}Dao{
//add all your dao template
}

Similarly you can do it for properties.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top