How to Avoid Annotations in POJOs
hibernate, java, jpa, mongodb, spring
Solution
When using JPA, you can leave you classes untouched and have ALL your configuration in XML files. Many people prefer annotations, but if changing the persistence implementation is a requirement, you should consider using external configuration.
I am not sure about other frameworks/specs besides JPA, but XML configuration goes a long way in java. I am sure, many frameworks offer such possibilities.
There is also a pattern called DTO (data transfer objects) that can be used for separation of persistence concerns from business concerns.
The gist is: you use your annotated, DB-centric classes for your DB connection only. Your main application only uses business-oriented classes and is persistence-agnostic. The data could come from a DB or from a flat file, as long as you can convert it to you business objects, all is well.
EDIT: DTOs sound like a lot of work, but you gain clarity and testability by separating concerns. hexagonal architecture and clean architecture emphasize this approach.
Problem
lets say i have the following POJO Class ``` public class Example { private String name; private int id; private Object o; // more fields // getter/Setter ``` Now lets assume i want to persist my Entity using JPA i will come on with the following example POJO Class: ``` @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "ID") private int id; @OneToMany(mappedBy = "directive") private String name; ``` In my Opinion this is bad, becaus if i want to use e.g. Spring Data MongoDB the Annotations would be useless/false. The only why i can think of to avoid this, is defining an Interface or an Abstract class, for example Storable, which defines getter/setter methods. But then i have violated the POJO definition (and one can argue it was not a Pojo to begin with). Are there any best Practices for defining Model Classes?