java - 如何阻止 hibernate 工具实现 Serialized 接口(interface)

标签 java hibernate serialization reverse-engineering hibernate-tools

我正在使用 hibernate 工具生成实体,映射到表。默认情况下,生成的每个实体类都实现 Serialized 接口(interface)。我们如何才能真正阻止该工具实现 Serialized 接口(interface)。下面是该工具生成的示例测试类。默认情况下它实现了 Serialized 接口(interface)。我的TestInterface实际上是扩展Serialized接口(interface),所以我不希望它是测试类。

public TestInterface extends Serializable {

}

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

而且在实体的顶部,我们只是将 name 属性添加到 @Table 注释中。我还需要该工具将架构属性添加为 schema="SampleUser"。我还需要将serialVersionUID 添加到代码中。下面是我需要该工具生成的代码。

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

谢谢。

最佳答案

您可以按照以下方法解决您的两个问题

问题 1:删除 pojo 生成中的实现可序列化接口(interface)。

分辨率:
Hibernate工具任务使用PojoTypeDeclation.ftl生成pojo类声明。它看起来像

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

如果您在此ftl中删除${pojo.getImplementsDeclaration()},那么您将不会在每个类中找到实现序列化

另一种方法是,您需要扩展 hibernate EntityPojoComponentPojo 类,并且需要重写您的自定义实现的 getImplementsDeclaration() 方法。

问题 2:表注释中缺少 Schema 属性。

分辨率: 使用HibernateToolTask​​生成Pojo会自动在Table注解中生成Schema属性。Hibernate工具任务内部使用Ejb3TypeDeclaration.ftl在每个生成的类中添加Table注解。

<#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>

仅当您的数据库具有架构概念时,它才会添加架构属性 并且您的表是任何模式的一部分。

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..

..

关于java - 如何阻止 hibernate 工具实现 Serialized 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26471161/

相关文章:

java - Mockito如何进行条件验证?

java - 数据截断 : Incorrect datetime value

java - 为什么 RxNetty 使用线程池?

java - 如何反转 FirebaseUI-Android 获取的数据?

java - hibernate Ant 工具

c# - WCF 查询对象数组

python - UUID 不是 JSON 可序列化的(转储数据)

c# - 首先为 EF 代码的 CTP5 关闭 ProxyCreationEnabled 有什么缺点

java - Maven 和 EJB3 - 控制客户端的名称

Hibernate二级缓存<<net.sf.ehcache.hibernate.EhCacheProvider>>