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

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

问题描述

我有一个关于 Hibernate 使用的简单问题.我一直看到人们以两种方式之一使用 JPA 注释,方法是对类的字段进行注释,另一种是在相应的 bean 上注释 get 方法.

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.

我的问题如下:使用@Id等JPA注解注解字段和bean方法是否有区别.

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

示例:

@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 - 哪个更好,字段或属性访问?

Spring 偏好是字段访问.这就是我所遵循的.

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

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

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