java beans: difference between persistent field an

2019-04-14 15:19发布

I got the impression that if we use persistent fields, there is no need for getter methods since the entity manager references the instance variables directly. However, when I removed the getter and setter methods from an entity to have persistent fields, the values for the corresponding instance variable was not retrieved from the database!

Does that mean we must have getter and setter methods even though we have persistent fields?

3条回答
成全新的幸福
2楼-- · 2019-04-14 15:54

If the entity class uses persistence, fields Persistence accesses the entity class instance variables directly at runtime.

While on persistence property, there is a getter and a setter method for each property.

What you said should have worked on hibernate.

A link about it

查看更多
淡お忘
3楼-- · 2019-04-14 16:00

You don't mention what JPA implementation you are using. Hibernate certainly allows you to access fields directly. However, it is not the default setting, so you have to specify this behavior in the mapping files with access=field. (with annotation based configuration, I would imagine you just need to annotate the fields directly ...)

查看更多
Melony?
4楼-- · 2019-04-14 16:05

What provider is that? I'd expect it to work. Anyway, just create protected getter and setter methods as a workaround if your provider needs that.

查看更多
登录 后发表回答