简体   繁体   English

Hibernate不承认持久的getter / setter属性

[英]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.

相关问题 当有吸气剂时,Hibernate是否总是需要一个setter? - Does Hibernate always need a setter when there is a getter? Kotlin 中预期的属性 getter 或 setter - Property getter or setter expected in Kotlin Bean 属性 'xxx' 不可读或具有无效的 getter 方法:getter 的返回类型是否与 setter 的参数类型匹配? - Bean property 'xxx' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter? Bean属性“ cmpcode”不可读或具有无效的getter方法:getter的返回类型是否与setter的参数类型匹配? - Bean property 'cmpcode' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter? Bean属性'xxxx不可读或具有无效的getter方法:getter的返回类型是否与setter的参数类型匹配 - Bean property 'xxxx is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter 带 getter 但不带 setter 的休眠实体属性:PropertyNotFoundException - Hibernate entity attribute with getter, but without setter: PropertyNotFoundException 休眠auto_increment getter / setter - hibernate auto_increment getter/setter 为什么默认方法不被识别为属性(getter/setter)? - why default method not recognized as property(getter/setter)? 有界通配符类型属性的获取器和设置器 - Getter and setter for bounded wild card type property Bean属性“ trustStore”不可写或具有无效的setter方法。 setter的参数类型是否与getter的返回类型匹配? - Bean property 'trustStore' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM