jeremy-vdw
jeremy-vdw

Reputation: 91

make sure JPA uses setters

I'm using JPA for my entity classes but JPA doesn't execute the setters for my parameters. I got this class :

@Entity
public class Groep implements Serializable, IGroep {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private int groepID;
  private String naam;
  @Column(name = "MOTIVATIE", length = 1000)
  private String motivatie;

  protected Groep(){

  }

  @Override
  public String getMotivatie() {
     return motivatie;
  }

  public void setMotivatie(String motivatie) {
    System.out.println("domein.Groep.setMotivatie()");
    this.motivatie = motivatie;
  }
}

But I never see the system.out... What is the problem?

Upvotes: 4

Views: 1128

Answers (1)

Maciej Kowalski
Maciej Kowalski

Reputation: 26572

The access type depends on where you place the annotations. You have placed them on instance variables so the persistence provider will try to access them directly by reflection.

If you are using hibernate as you persistence provider, you can simply define your @Id this way:

As a JPA provider, Hibernate can introspect both the entity attributes (instance fields) or the accessors (instance properties). By default, the placement of the @Id annotation gives the default access strategy.

Just keep in mind that you should be consistent in your annotation placement strategy regarding all the entities.

Upvotes: 2

Related Questions