views:

319

answers:

1

Although I'm a fan of using annotations, I do have concern over the dependencies they create on third party jars.

For the purpose of this question, I'm referring specifically to Hibernate or JPA annotations on domain model classes.

In reality, I want my domain model to consist of simple POJOs without any dependencies on a persistence framework. If I deploy my domain classes in a single jar, I don't see why an application that uses that jar should have to import and any dependencies relating to the annotations used.

Is it possible that the annotations jars need only be a compile-time dependency?

Thanks,

Andrew

A: 

It really depends on what you're trying to achieve. Avoiding dependency for the sake of avoiding dependency doesn't make much sense. In this case, aren't you still going to persist your entities? So dependency is still going to be there.

Now, if we are talking about API being deployed separately (and actual persistence happening in some other layer deployed separately), you do have some options:

  1. You can define your API via interfaces (which is not a bad thing to begin with, annotations or not - makes for easy layer separation / testing / what have you) and only annotate the actual implementations. Deploy your API as a separate jar and you have no dependencies.

  2. If you define your API as concrete entities, your best option to eliminate dependencies is to use XML mappings instead of annotations. Take a look at this question for details. I would NOT do this, to be honest. JPA is part of JavaEE; it's not really a dependency to be concerned about.

ChssPly76