0

If we make a column Id which corresponds to id of User, then why don't we put it near the variable id instead of the getter for id ? I got the answer to that here - Where to put hibernate annotations?.

But, because my book put it near the getter, it looks like some class will need to access this object via getter to serialize/persist it to a database. What is this class and how is does it perform the persistence ? Do I call its methods to do the persistence ?

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class User {
  private Long id;
  private String password;

  @Id
  @GeneratedValue
  public Long getId() {
    return id;
  }

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

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }
}
Community
  • 1
  • 1
david blaine
  • 5,683
  • 12
  • 46
  • 55

1 Answers1

1

Hibernate will use reflection to find the appropriate methods or fields on your class to read. This is why Hibernate is able to read private fields.

The code that does this reflection is called from session.save(Object object).

jsha
  • 602
  • 6
  • 16