JAXB & JPA annotations. Are there any good arguments for choosing access to object access properties?

I am starting a new project using JAXB and JPA annotations. I read with interest the posts discussing the pros and cons of mixing JAXB and JPA annotations in the same class.

Are there any recommendations that describe when to use field level annotations and when to use property level annotations or any arguments for a general choice of one approach over another? I believe field level annotations are cleaner and more readable; they are usually located at the top of the class and make it easy to understand how each field is presented in different contexts. However, besides this readability argument, are there any other things to consider when deciding where to comment?

+4
source share
2 answers

JPA often uses lazy loading over properties. If you use property access in JAXB, then it will trigger these lazy properties during the marshal.

Also to support lazy loading (or change tracking, etc.). Some JPA implementations use byte code processing to inject fields into classes to enable this. Using field access with JAXB can lead to errors when using this type of JPA implementation.

Some reference materials related to using JAXB with JPA entities:

+3
source

Another advantage is that you do not need to expose all fields as properties. If there is no good reason, I usually prefer field level annotations.

+1
source

Source: https://habr.com/ru/post/1335872/


All Articles