JAXB & JPA annotations. Are there any strong arguments for preferring field access over property access?
I'm starting a new project th开发者_运维百科at's using both JAXB and JPA annotations. I've read with interest the posts discussing the pros and cons of mixing both JAXB and JPA annotations in a single class.
Are there any guidelines describing when to use field level annotations and when to use property level annotations, or any arguments for generally choosing one approach over the other? I find field level annotations cleaner and more readable; they're usually found at the top of the class and make it easy to figure out how each field is represented in varying contexts. Aside from this argument concerning readability however, are there any other things to consider when deciding where to annotate?
JPA often uses lazy loading on properties. If you use property access in JAXB then it will trigger these lazy properties during a marshal.
Also in order to support lazy loading (or change tracking, etc). Some JPA implementations use byte code manipulation to introduce fields onto classes to enable this. Using field access with JAXB can cause errors when using this type of JPA implementation.
Some reference material related to using JAXB with JPA entities:
- http://bdoughan.blogspot.com/2010/07/jpa-entities-to-xml-bidirectional.html
- http://wiki.eclipse.org/EclipseLink/Examples/MOXy/JPA
Another advantage is, that you don't have to expose all fields as properties. If there is not a good reason to do otherwise I generally prefer field level annotations.
精彩评论