• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mixing JPA and JAXB on the same POJO is a bad idea?

 
Luke Murphy
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, say you need to persist a Domain Object and you also need to convert it to XML.

I am thinking wow JPA and JAXB look cool for this. But I also think it would be bad practise to mix the annotations in the same POJO.
It would be confusing and potentially problematic. On the other hand you could argue having two different representations for the same Domain Object is repetitive and a bit redundant.

These guys favour mixing the annotations:
http://stackoverflow.com/questions/1820428/do-domain-classes-usually-get-jpa-or-jaxb-annotations-or-both

I don't. I'd prefer some redundancy for a clean separation.

Anyone any views on this?
 
Ayan Dave
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clean separation will be good only if your domain model allows it - which in many cases it won't. You don't want redundant domain objects floating around in your system that can be misused without your knowledge.

I agree that mixing annotation may make the code less readable but I won't add redundant domain objects. One option may be to use AspectJ for adding the orm or oxm behaviour to the objects. Spring Roo creates domain objects such that the toString methods are placed in aspects around the domain objects. May be some technique like that may work here.
 
Luke Murphy
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ayan Dave wrote:Clean separation will be good only if your domain model allows it - which in many cases it won't. You don't want redundant domain objects floating around in your system that can be misused without your knowledge.

I agree that mixing annotation may make the code less readable but I won't add redundant domain objects. One option may be to use AspectJ for adding the orm or oxm behaviour to the objects. Spring Roo creates domain objects such that the toString methods are placed in aspects around the domain objects. May be some technique like that may work here.


I disagree. I don't think it's anything to do with domain model. That's a higher level thing.

Personally I favour the separation. A factory / builder or mapper class could be used to map from one to the other.
 
Gaurav Tripathi
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is my thought on JPA & JAXB:

You can create a base (interface or abstract class) POJO class and each persistence class (domain) should extend that class for persistence (use the JPA). Other class (also extend the base class) would be for conversion to-from XML and will use the JAXB. Advantage would be clean separation of design and drawback of this design can be maintainability.
 
k space
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Luke Murphy wrote:
Personally I favour the separation. A factory / builder or mapper class could be used to map from one to the other.


I agreed that a clean separation is important in terms of maintainability and extendability. In your case, you need POJO + JPA for persistence, and JAXB for Transfer Object or a presentation of your Domain Object. By separating them (as Murphy suggested using a factory/builder to generate the required object), you have the flexibility of supporting different presentations, e.g. JSON.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic