Hibernate / JPA - 注释bean方法vs字段 [英] Hibernate/JPA - annotating bean methods vs fields

查看:80
本文介绍了Hibernate / JPA - 注释bean方法vs字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于Hibernate使用的简单问题。我一直以两种方式之一看到使用JPA注释的人注释类的字段,并注释相应bean的get方法。



我的问题是如下所示:注释字段和bean方法与JPA批注(如@Id)之间是否存在差异。


$ b 示例:

  @Entity 
public class User
{

** @ ID **
private int id;

public int getId(){
return this.id;
}

public void setId(int id){
this.id = id;
}

}

------- ---- OR -----------

  @Entity 
public class User
{


private int id;

** @ ID **
public int getId(){
return this.id;
}

public void setId(int id){
this.id = id;
}

}


解决方案

是的,我相信你想在字段和属性访问上进行搜索:



Hibernate Annotations - 哪个更好,字段或属性访问?

Spring首选项是字段访问权限。这就是我所追求的。


I have a simple question about usage of Hibernate. I keep seeing people using JPA annotations in one of two ways by annotating the fields of a class and also by annotating the get method on the corresponding beans.

My question is as follows: Is there a difference between annotating fields and bean methods with JPA annoations such as @Id.

example:

@Entity
public class User
{

**@ID**
private int id;

public int getId(){
return this.id;
}

public void setId(int id){
this.id=id;
}

}

-----------OR-----------

@Entity
public class User
{


private int id;

**@ID**
public int getId(){
return this.id;
}

public void setId(int id){
this.id=id;
}

}

解决方案

Yes, I believe you want to search on field versus property access:

Hibernate Annotations - Which is better, field or property access?

The Spring preference is field access. That's what I follow.

这篇关于Hibernate / JPA - 注释bean方法vs字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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