Design question: annotating entities

View: New views
2 Messages — Rating Filter:   Alert me  

Design question: annotating entities

by hbf :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi everybody,

I have a very basic question regarding JPA annotations.

Some of my JPA-annotated entities are constructed in user code, eg. to  
create a new instance or a filter object for a query call. I want to  
make sure that later on I can replace my persistence layer. How can I  
achieve this?

What comes to mind is:

- Entities are classes with JPA annotations: Bad (dependency on JPA)
- Entities are classes but instead of JPA annotations we use XML  
configuration: works with JPA (but not necessarily with other frameworks)
- Entities are interfaces and there are JPA-specific implementations  
with (or without) annotations

The third approach seems fine except that in user code you cannot
construct entity instances anymore without a hard dependency on your
implementation classes. Do you use a factory for that (in a related
service?) or is there some other solution around that you recommend?

Thanks,
Kaspar

Re: Design question: annotating entities

by James Sutherland :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I would recommend you keep it simple, and just use pojo classes with JPA annotations or orm.xml.  I see no issue having the JPA annotations in your classes, if you want to switch persistence solutions you can just use another JPA provider, it is the standard.  Although using an orm.xml is probably easier for deployment, so you can make changes without recompiling code.

You could use interfaces if you desire, and just use factories to create the classes.  I would avoid wrapping the JPA API's in your own wrapper persistence layer, why bother wrapping standard API's with your own proprietary ones...