如何将hibernate注释放置在字段上,但始终具有属性访问权限? [英] How can I place the hibernate annotations on the fields, but always have property access?

查看:122
本文介绍了如何将hibernate注释放置在字段上,但始终具有属性访问权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在问,因为我想为getters / setter使用代码生成。



还因为我更喜欢映射注释出现在顶部



我想知道这种方法是否正确:

  @Entity 
@Table(name =test)
// @Access(AccessType.PROPERTY)//不能使用它,因为hibernate抱怨没有
public class Test实现Serializable
{
@Id
//我希望这会导致所有其他
//属性的属性访问,但我不确定如何确认这个...
@Access(AccessType.PROPERTY)
@Column(name =id)
private long id = 0;

@Column(name =test_string)
private String testString = null;

...

更新:我刚才测试了上面的例子看起来像使用字段访问来访问'testString'属性。我向getter / setter添加了日志记录,并且他们从未被调用过。另一方面,当我在'testString'字段中添加@Access(AccessType.PROPERTY)时,调用getter和setter方法。



所以目前它看起来像我唯一的选择是在每个字段前面写@Access(AccessType.PROPERTY):-(

解决方案

1)财产访问不是坚持实体的正确方法。你持久化状态,状态存储在字段中。

2)您可以使用 @Access 注释设置要使用的访问类型为一个类。


I am asking because I would like to use code generation for the getters/setter.

And also because I would prefer the mapping annotations to appear at the top of the class, where I declare the fields.

I wonder if this approach is correct:

@Entity
@Table(name = "test")
// @Access(AccessType.PROPERTY) // cannot use this, because hibernate complains that no 
public class Test implements Serializable
{
  @Id
  // I hope this results in property access for all of the other
  // properties as well, but I am not sure how to confirm this...
  @Access(AccessType.PROPERTY) 
  @Column(name = "id")
  private long                id         = 0;

  @Column(name = "test_string")
  private String              testString = null;

  ...

Update: I just tested the above example and it looks like the 'testString' property is accessed using field access. I added logging statements to the getter/setter and they were never called. On the other hand, when I added @Access(AccessType.PROPERTY) also to the 'testString' field, the getter and setter methods were called.

So at the moment it looks like my only option is to write "@Access(AccessType.PROPERTY)" in front of every field :-(

解决方案

1) Property access isn't the right way to persist entities. You persist state, and state is stored in fields.

2) You can use the @Access annotation to set the access type to use for a class.

这篇关于如何将hibernate注释放置在字段上,但始终具有属性访问权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆