[英]Is it possible to persist a DSL Java object with JPA?
I have a DSL Java object, ie a POJO which returns this
in setters plus the getters/setters have an unusual naming pattern: 我有一个DSL Java对象,即POJO,
this
以设置方法返回this
对象,而getter / setter方法具有不寻常的命名模式:
public class Demo {
private long id;
private String name;
private Date created;
public Demo id (long value) { id = value; return this; }
public String id () { return id; }
public Demo name (String value) { name = value; return this; }
public String name () { return name; }
public Demo created (Date value) { created = value; return this; }
public Date created () {
if (created == null) created = new Date ();
return created;
}
}
Is it possible to tell JPA to use "name(String)" and "name()" as the setter/getter method? 是否可以告诉JPA将“ name(String)”和“ name()”用作设置器/获取器方法?
[EDIT] My issue is the created
field above. [编辑]我的问题是上面的
created
字段。 For this field, I want JPA to use the "getter" created()
so the field will always be non-NULL. 对于此字段,我希望JPA使用“ getter”
created()
因此该字段将始终为非NULL。
Or is there a way to tell JPA to use CURRENT TIMESTAMP
when creating a new object with created == null
? 还是有一种方法可以让JPA在创建带有
created == null
的新对象时使用CURRENT TIMESTAMP
?
According to the JPA spec (see JSR-220 ) chapter 2.1.1 you can tell JPA to use field access instead of property access by annotating the fields for mapping information and not the getter methods. 根据JPA规范(请参阅JSR-220 )的第2.1.1章,您可以通过注释字段以映射信息而不是getter方法来告诉JPA使用字段访问而不是属性访问。
I don't think you can tell JPA which naming convention to use for getters and setters since it's a basic java beans concept. 我认为您不能告诉JPA哪种命名约定用于getter和setter,因为它是Java Bean的基本概念。
您是否不能简单地初始化在类中定义时created
的内容,然后使用字段访问。
private Date created = new Date();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.