生成在使用 @Entity 注解的类中生成 getter 和 setter 的作用是什么?

huangapple go评论95阅读模式
英文:

What is the use of generating getters and setters in a class annotated with @Entity (Hibernate)?

问题

在使用 @Entity 注解的类中生成 getters 和 setters 的作用是什么?它们在什么时候被调用?

@Entity
@Table(name="student")
public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    private int id;
    private String firstName;
    private String lastName;
    public Student() {
    }

    public Student(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    //other getters and setters
}
英文:

What is the use of generating getters and setters in a class annotated with @Entity (Hibernate)?
When do they get invoked?

@Entity
@Table(name="student")
public class Student {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name="id")
	private int id;
	private String firstName;
	private String lastName;
	public Student() {
	}

	public Student(String firstName, String lastName) {
		this.firstName = firstName;
		this.lastName = lastName;
	}

	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
//other getters and setters
}

答案1

得分: 0

当您的字段变为私有时,您需要使用getter和setter来在实体类外部访问它们。

英文:

As soon as your fields are private then you need getters and setters to access them outside of the entity class.

答案2

得分: 0

默认情况下,Hibernate使用public的getter和setter来访问实体的属性,这被认为是一种常见的方法。

然而,Hibernate并不严格要求这样做。如果你的字段是public的,那么你可以告诉Hibernate直接访问这些字段(通过access选项)。另一方面,如果字段、getter或setter是privatedefaultprotected的,Hibernate也可以处理它们(通过反射,我认为)。

来自Hibernate参考文档:

2.1.3. 为持久属性声明getter和setter

标准的、可移植的JPA基本上需要这样做。否则,您的模型将违反上述要求,不允许从实体外部直接访问实体的持久状态字段。

尽管Hibernate不要求这样做,但建议遵循JavaBean约定,为您的实体的持久属性定义getter和setter。您仍然可以告诉Hibernate直接访问实体的字段。

属性(无论是字段还是getter/setter)不需要声明为public。Hibernate可以处理以public、protected、package或private可见性声明的属性。再次强调,如果希望使用运行时代理生成进行延迟加载,getter/setter的可见性至少应为package可见性。

英文:

By default Hibernate uses public getters and setters to access entity's attributes, which is considered to be a common approach.

However, it is not strictly required by Hibernate. If your fields are public, then you can tell Hibernate to access those fields directly (through access option). In other hand, having private, default or protected fields/getters/setters, Hibernate is able to deal with them too (through refection, I think).

From Hibernate reference:

> 2.1.3. Declare getters and setters for persistent attributes

> Standard, portable JPA essentially requires this. Otherwise your model would
> violate the requirement quoted above in regards to accessing the
> entity persistent state fields directly from outside the entity
> itself.
>
> Although Hibernate does not require it, it is recommended to follow
> JavaBean conventions by defining getters and setters for you entities
> persistent attributes. You can still tell Hibernate to directly access
> the entity's fields.
>
> Attributes (whether fields or getters/setters) need not be declared
> public. Hibernate can deal with attributes declared with public,
> protected, package or private visibility. Again, if wanting to use
> runtime proxy generation for lazy loading the visibility for the
> getter/setter should be at least package visibility.

huangapple
  • 本文由 发表于 2023年2月19日 02:03:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495340.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定