java beans: difference between persistent field and persistent property?

被刻印的时光 ゝ 提交于 2019-12-06 10:59:18

问题


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?


回答1:


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




回答2:


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 ...)




回答3:


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.



来源:https://stackoverflow.com/questions/1729070/java-beans-difference-between-persistent-field-and-persistent-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!