-1

I will write a little example code which generates the compiler warning in the header:

@Entity class User {

    private String passwordHash;

    public User() {}

    public User(String passwordHash) {
        this.passwordHash = passwordHash;
    }

    @Column(name="PASSWORD")
    public String getPasswordHash() {
        return passwordHash;
    }

    private void setPasswordHash(String passwordHash) {
        this.passwordHash = passwordHash;
    }
}

I'm currently using hibernate only for filling the database, but later on I will only use it for reading most of the time.

I cannot change it to private final passwordHash because then I would have to initialize it in the default Constructor.

My Question now is, what could happen if I ignore the warning, or how can I get rid of it without making the the SETTER public or protected? I thought hibernate can get it through reflection even if it is private (Like described here)?

UPDATE:

I tested it with deleting the private setter. Hibernate is using it via reflection, so the setter has to be there. But since it is a password hash, I cannot change the visibility to public. Protected would be okay, but I would not be happy because in my eyes it isn't a good solution.

Community
  • 1
  • 1
Pascal Schneider
  • 405
  • 1
  • 5
  • 19
  • Why you even have a setter that is obviously not used? Remove it! – Seelenvirtuose Dec 08 '15 at 08:56
  • Because I thought Hibernate need the setter method to map the value from the database to the object. Why don't you post this as an answer? The Warning is gone now, but I'm not sure if hibernate works correctly, I will test it. – Pascal Schneider Dec 08 '15 at 09:08
  • I tested it with my actual code and I get the Exception `PropertyNotFoundException: Could not find a setter for property passwordHash in class User`. The setter is used with reflection. – Pascal Schneider Dec 08 '15 at 09:17
  • 1
    There seems to be something wrong in your class. If you have [access type set to fields](http://stackoverflow.com/questions/13874528/what-is-the-purpose-of-accesstype-field-accesstype-property-and-access) (which is derived by having annotations on fields) then you don't need getters and setters at all. The error message you get indicate that the access type is set to `AccessType.PROPERTY`. Maybe some older versions of Hibernate always required a setter, but I'm not sure. – Seelenvirtuose Dec 08 '15 at 10:43
  • FWIW some JPA implementations don't need setters/getters when you are using a persistent FIELD (even when that is private) – Neil Stockton Dec 08 '15 at 10:44
  • Okay I don't know why, but this particular example gave me that warning. First I had the annotations before the getters, but I also had the warning when I changed it to field access. Now I do net get the warning anymore, after I changed my actual code to field access. But I'd like to have the access through setter because it is more overseeable in my eyes. But okay I guess I have to use field access when I don't want a public setter. Thanks everyone for your help – Pascal Schneider Dec 08 '15 at 11:59
  • If you really have the annotation on your gettes, then indeed you also need the setters. This is a JPA requirement. But that also means that the described problem and your code do not match. -1 – Seelenvirtuose Dec 08 '15 at 12:11

2 Answers2

1

When you have a private member variable in your class you need to have a public setter for the same, so that any manipulation to the state of the variable can be done through the said Method.

So you would require to have a public method for the same.

Vivek Singh
  • 2,047
  • 11
  • 24
  • The thing is, I can't give an opportunity to change a password hash through a setter method. Any person using my code could simply create an own hash, and change it via this setter method, which would make to password useless. – Pascal Schneider Dec 08 '15 at 09:18
  • So in that case always give a clone to your variable – Vivek Singh Dec 08 '15 at 09:20
1

Changed to only give warning if the setter is private. It doesn't make sense to have a private setter.

See the workflow here

Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
  • 1
    That is the point, it makes sense! Hibernate uses the private setter through reflection! I don't want any other class to do anything with my password, but I want hibernate to get it from the database, so making it private is the best way. – Pascal Schneider Dec 08 '15 at 09:37
  • @KrazyKalle why you need to add setter at all, when it is not required, you still ot understood the concept behind Hibernate. – Ankur Singhal Dec 08 '15 at 09:39
  • @ankur-singhal sometimes we need to map using getter and setter. – O.Badr Jul 24 '17 at 11:21