我使用eclipse juno的Hiberate Tool插件生成java bean映射数据库表
它创建java文件(例如):
@Entity
@Table(name = "ACTIVITIES")
public class Activities implements java.io.Serializable {
private ActivitiesId id;
private String title;
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "id", column = @Column(name = "id", nullable = false)),
@AttributeOverride(name = "codiceCreatore", column = @Column(name = "codice_creatore", nullable = false))
})
public SynchActivitiesDTOId getId() {
return this.id;
}
public void setId(SynchActivitiesDTOId id) {
this.id = id;
}
@Column(name = "title", nullable = false)
public String getTitle() {
return this.title;
}
public void setTitolo(String title) {
this.title = title;
}
}
我喜欢在属性或get方法上写@NotNull注释:
@Entity
@Table(name = "ACTIVITIES")
public class Activities implements java.io.Serializable {
@NotNull
private ActivitiesId id;
@NotNull
private String title;
...
}
我尝试编辑Ejb3PropertyGetAnnotation.ftl (在C:\ Program Files \ eclipse \ plugins \ org.hibernate.eclipse.libs_3.4.0.v20120611-2017-H93-Final \ lib \ tools \ hibernate-tools.jar \ pojo) 加入
<#if !property.optional>
@${pojo.importType("org.hibernate.validator.NotNull")}
</#if>
但在Activities.java中没有任何变化
我该怎么做?
提前致谢
编辑: 我试着修改 C:\ Program Files \ eclipse \ plugins \ org.hibernate.eclipse.libs_3.4.0.v20120611-2017-H93-Final \ lib \ tools \ hibernate-tools.jar \ pojo \ PojoFields
<#foreach field in pojo.getAllPropertiesIterator()><#if pojo.getMetaAttribAsBool(field, "gen-property", true)> <#if pojo.hasMetaAttribute(field, "field-description")> /**
${pojo.getFieldJavaDoc(field, 0)}
*/
</#if> ${pojo.getFieldModifiers(field)} ${pojo.getJavaTypeName(field, jdk5)} ${field.name}<#if pojo.hasFieldInitializor(field, jdk5)> = ${pojo.getFieldInitialization(field, jdk5)}</#if>;
</#if>
+ <#if !field.optional>@${pojo.importType("org.hibernate.validator.NotNull")}</#if>
</#foreach>
但没有任何改变。
答案 0 :(得分:0)
您可以通过hibernate reverse engineering xml进行调整(非常实用)。 Eclipse hibernate工具可以让你指定一个。
例如,对于您的表,请为名为id的列包含以下元属性:
<hibernate-reverse-engineering>
<table-filter package="my.package" match-name="ACTIVITIES" />
<table name="ACTIVITIES">
<meta attribute="extra-import">javax.validation.constraints.NotNull</meta>
<column name="titolo">
<meta attribute="scope-field">@NotNull private</meta>
</column>
</table>
</hibernate-reverse-engineering>
该字段将使用我们为“scope-field”属性提供的限定符作用域。在那里我们调整我们需要的任何额外限定符。在此示例中,我们将其标记为private,并另外添加@NotNull限定符。请参阅上面使用的元属性以进行额外导入。
有关元属性的完整列表,请参阅此link
HTH。