[英]Hibernate does not acknowledge persistent getter/setter property
I have this propery in my entity class: 我的实体类中有这个属性:
@Column(name="avatar",nullable=false,length=1000)
String getAvatarData() {
return new JSONObject(avatar.export()).toString();
}
void setAvatarData(String data) {
avatar = Avatar.restore(new JSONObject(data).toMap());
}
Hibernate doesn't handle it at all. Hibernate根本不处理它。 At least, it's not included in the schema it generates.
至少,它不包含在它生成的模式中。
@Access(AccessType.PROPERTY)
on your entity. 在你的实体上。 That's JPA 2.0.
这是JPA 2.0。 For 1.0, use
org.hibernate.AccessType
: 对于1.0,使用
org.hibernate.AccessType
:
@AccessType("property")
By the way, I would rather have a simple field with getters and setters, and annotate the field instead. 顺便说一句,我宁愿有一个带有getter和setter的简单字段,而是注释该字段。 Then, if you want custom transformations, add other methods, like
getFooAsJSON
然后,如果您想要自定义转换,请添加其他方法,例如
getFooAsJSON
To clarify Bozho's answer: in JPA 2.0 (Hibernate 3.5 and above) you declare a single field with property access as following: 澄清Bozho的答案:在JPA 2.0(Hibernate 3.5及更高版本)中,您声明一个具有属性访问权限的字段,如下所示:
@Access(AccessType.FIELD)
public class Foo {
...
@Access(AccessType.PROPERTY)
@Column(name="avatar",nullable=false,length=1000)
String getAvatarData() { ... }
void setAvatarData(String data) { ... }
}
in previous versions of Hibernate - as following (note that annotations are still placed on the field): 在以前版本的Hibernate中 - 如下所示(注意注释仍然放在字段上):
@Access("field")
public class Foo {
...
@Access("property")
@Column(name="avatar",nullable=false,length=1000)
private Avatar avatarData;
String getAvatarData() { ... }
void setAvatarData(String data) { ... }
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.