2

I would like to use JPA entity classes as simple POJOs /data holder objects to be passed to other layers/clients of enterprise application.

Can this be done without the other layers/clients having JPA API jar on classpath - (just having jar containing entity classes)? Or do they need this to resolve things like:

@OneToMany(mappedBy="owner",fetch=FetchType.EAGER) in Entity source code?

If JPA API is needed in the client/presentation layer which is just intended to work with the entity as POJO, what is the best solution/alternative to avoid this necessity of having JPA API depedency?

EDIT: Just to make it more clear. I am asking about possible solutions outside java ee enviroment where JPA API is not at reach. Can I avoid coding DTOs for every Entity class I want to send to standard java client without JPA API?

ps-aux
  • 11,627
  • 25
  • 81
  • 128

1 Answers1

1

If you share your Entity classes with other applications which don't have the persistence API on their classpath, then they will fail loading your classes. That's because the JVM won't be able to resolve the annotation classes.

However, if we are talking about enterprise applications in the sense of Java EE, then they should all have the persistence API on their classpath, since it's part of the standard. The API is provided by all Java EE application servers. In that case, put your entity classes in a JAR file inside the EAR's lib/ directory, so it's shared with all modules of your project.

References:

Community
  • 1
  • 1
nif
  • 3,342
  • 20
  • 18
  • That's what I was exactly asking about thanks. I know that inside the server it works. But I am interested in solution for external java clients without JPA API on their classpath. So I have to create special DTO for every entity I intend to serialize and send somewhere where JPA API is not present? – ps-aux Jul 07 '13 at 12:05
  • That would be one option. To avoid duplicate code, you could configure your JPA entities using XML instead of annotations. That way, they are really POJOs without annotations and can be shared. For an example how this looks like, see [this question](http://stackoverflow.com/questions/13913905/configuring-persistence-and-orm-with-jpa-2). You can even mix annotation- and XML-based configuration, if you like. – nif Jul 07 '13 at 12:30
  • Ok. Thank you. This confirmed my assumptions and possible options how to handle the situation. – ps-aux Aug 03 '13 at 09:17