Monday, February 9, 2009

JPA field access vs property access

Here are the two examples of field access and property access.
Field access
private Company publisher;

Property Access
private Company getPublisher() { ... }

When using property access, only the getter and setter method for a property should ever access the underlying persistent field directly. Other methods, including internal business methods in the persistent class, should go through the getter and setter methods when manipulating persistent state.
Also, take care when adding business logic to your getter and setter methods. Consider that they are invoked by the persistence implementation to load and retrieve all persistent state; other side effects might not be desirable.

Each class must use either field access or property access for all state; you cannot use both access types within the same class.
Additionally, a subclass must use the same access type as its superclass.

No comments:

Post a Comment