1

I'm using hibernate tools to generate the entities, mapping to the tables. Each entity class that is generated is implementing the Serializable interface by default. How can we actually stop the tool from implementing Serializable interface. Below is an example Test class which was generated by the tool. By default it implemented the Serializable interface. My TestInterface actually extending Serializable interface, so I do not want it it Test Class.

public TestInterface extends Serializable {

}

@Entity
@Table(name = "TEST")
public class Test implements TestInterface, java.io.Serializable {
..
..    
}

And also on the top of Entities, we just have name attribute added to the @Table annotation. I also need the tool to add the schema attribute as schema="SampleUser". I also need to add serialVersionUID to the code. Below is the code which I need the tool to generate.

@Entity
@Table(name = "TEST", schema = "SampleUser")
public class Test implements TestInterface{
   private static final long serialVersionUID = 1L;
   ..
   ..    
}

Thank you.

devvapp
  • 143
  • 1
  • 2
  • 9

1 Answers1

0

You can follow below approaches for your two issues

Issue 1 : Removing Implmenting seralizable interface in pojo generation.

Resolution :
Hibernate tool task uses PojoTypeDeclation.ftl to generate pojo class declaration.It looks like

/**
${pojo.getClassJavaDoc(pojo.getDeclarationName() + " generated by hbm2java", 0)}
 */
<#include "Ejb3TypeDeclaration.ftl"/>
${pojo.getClassModifiers()} ${pojo.getDeclarationType()} ${pojo.getDeclarationName()} ${pojo.getExtendsDeclaration()} ${pojo.getImplementsDeclaration()}

If you remove ${pojo.getImplementsDeclaration()} in this ftl then you will not find implements serialization in every class.

Other way is, you need to extend hibernate EntityPojo and ComponentPojo class and needs to override getImplementsDeclaration() method your custom implementation.

Issue 2 : Schema attribute is missing in Table annotation.

Resolution: Pojo generation using HibernateToolTask will automatically generates Schema property in Table annotation.Hibernate tool task internally use Ejb3TypeDeclaration.ftl to add Table annotation in each generated class.

<#if ejb3?if_exists>
<#if pojo.isComponent()>
@${pojo.importType("javax.persistence.Embeddable")}
<#else>
@${pojo.importType("javax.persistence.Entity")}
@${pojo.importType("javax.persistence.Table")}(name="${clazz.table.name}"
<#if clazz.table.schema?exists>
    ,schema="${clazz.table.schema}"
</#if><#if clazz.table.catalog?exists>
    ,catalog="${clazz.table.catalog}"
</#if>
<#assign uniqueConstraint=pojo.generateAnnTableUniqueConstraint()>
<#if uniqueConstraint?has_content>
    , uniqueConstraints = ${uniqueConstraint} 
</#if>)
</#if>
</#if>

It will add schema property only if your Database has Schema concept and your table is part of any schema.

Eg : If you are using MySql,then
you will not get schema property because mysql DB dont have schema's
concept. Converse is true in Postgress,Hsql,Oracle..

..

Sunil Kumar
  • 5,477
  • 4
  • 31
  • 38
  • Four years later, but note that the template name is: PojoTypeDeclaration.ftl . See usage here: https://forum.hibernate.org/viewtopic.php?f=6&t=970472 or maybe using maven plugin: https://github.com/stadler/hibernate-tools-maven-plugin – Kjeld Aug 25 '19 at 20:21