我在这里要求一种简单的方法来在 Eclipse 从数据库生成的 JPA 实体中添加一些自定义代码。
基本上我想要实现的是添加包含实体属性名称的公共字符串属性,并在我需要提供“属性名称”作为字符串时使用它们,并确保不会出现运行时访问错误。
像这样的东西
@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";
}
Run Code Online (Sandbox Code Playgroud)
所以我可以使用属性名称
ClientModel.nameProperty
Run Code Online (Sandbox Code Playgroud)
并且在编译时确保他的存在以及在进一步生成实体后名称重构的情况下。
我知道 Telosys Tools & co. 的存在,但我希望可以有一些更简单/更快的东西(比如在使用 JAXB 生成 WSDL_to_entity 时作为插件提供的自定义类)
谢谢你。
最后我使用了 Telosys Tools,即使我不想在我的项目中添加另一个工具,设置起来也很容易,只需在这里阅读 https://sites.google.com/site/telosystools/getting -started/21-configure-a-project
在我的特定情况下,我在创建 getter 期间将此代码添加到模板“JPA_bean_with_links”中
#if ( $field.getter ) public static String ${field.getter}Property() {
return "$field.name";
}
#end
Run Code Online (Sandbox Code Playgroud)