我在这里寻求一种简单的方法,以在Eclipse从数据库生成的JPA实体中添加一些自定义代码。
基本上,我要实现的是添加包含实体属性名称的公共String属性,并在需要提供“属性名称”作为String并确保不会出现运行时访问错误时使用它们。
像这样
@Entity
@Table(name="clients")
@NamedQuery(name="ClientModel.findAll", query="SELECT c FROM ClientModel c")
public class ClientModel implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="id_client")
private long idClient;
public String name;
public ClienteModel() {
}
public long getIdClient() {
return this.idClient;
}
public void setIdClient(long idClient) {
this.idClient = idClient;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
//CUSTOM CODE
public static final String idClientProperty = "idClient";
public static final String nameProperty = "name";
}
所以我可以使用像
ClientModel.nameProperty
并且在他生存的时候以及在进一步的实体生成之后重构名称的情况下都要保证编译时的安全。
我知道Telosys Tools&co。的存在,但我希望可以简化一些/更快一些(例如,自定义类作为JAXB的WSDL_to_entity生成中的插件提供)
谢谢你。
最后,即使我不想在我的项目中添加其他工具,我也已经使用过Telosys Tools,有点容易设置,只需在这里阅读https://sites.google.com/site/telosystools/getting -started / 21-configure-a-project
在我的特定情况下,在吸气剂创建过程中,我已将此代码添加到模板“ JPA_bean_with_links”中
#if ( $field.getter ) public static String ${field.getter}Property() {
return "$field.name";
}
#end
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句