• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using same classes as JAXB annotated entities and JPA

 
Alexander Petrov
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hei, I have the case where my data is comming from external system web services. I am thinking store local the external data. I am thinking to use JaxB to parse the web services. I am thinking to use the same classes that are annotated with JaxB annotations and used as a model for the web services and annotate them with JPA annotations and use them to persist directly the data to the database. Theoretically I can have a second set of classes and mapper that is moving the data from the DTOs to the Entities,but I am questioning the needs since I think both JPA and JAxB are quite flexible and can co-exist.

What do you think ? Do you think this is a bad practice to mix JaxB and JPA(why I have the feeling that it is almost rhetorical question )?
 
K. Tsang
Bartender
Posts: 3583
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this for the assignment?
 
Alexander Petrov
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, it is. I am planning to use both JaxB and JPA , and wonder if I annotate the Pojoes with both of them or just keep two sets of porous plus a mapper. The thing is that while I believe it is probably better to have separation in real life very often I annotate the same pojo both with JPA and JaxB. Is the question inappropriate ? I think that it is not inappropriate question because it is very very general. Is it ok to combine JaxB with JPA annotations or not.
 
K. Tsang
Bartender
Posts: 3583
16
Android Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm hard to say.

If the web service is REST then I will assume the response is JSON and the JPA entities annotated with Jackson @JsonProperty

Apply this to REST xml response or SOAP web services, I think you can, but I sure don't know if this is a best practice or not, unlike with JSON.

Either way, documenting your decisions is key.
 
Tim Holloway
Saloon Keeper
Posts: 18359
56
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also define POJOs with both JAXB and JPA annotations. It allows me to have "one stop shopping" for data that can be imported/exported via multiple mechanisms and thus avoids dual maintenance.

If - as is often the case - there's not an exact correspondence between what columns and/or relationships need to be persisted depending on the persisistence mechanism, then it can require a little creativity, but so far, I've always managed to get what I want.
 
Alexander Petrov
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hei Tim, did you present this kind of model on the assignment or you are yet to finish the assignment ? Just wondering if you think it got accepted well. So far I have placed a Mapper in my assignment as a way to insurance myself if the reviewer see it in a negative way. Seems to me like the safest way to build it. I mean a reviewer may potentially find arguments agains co-existance of JaxB and JPA annotations but hardly will find any arguments agains the Mapper right ? What do you think ?
 
Tim Holloway
Saloon Keeper
Posts: 18359
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, this wasn't a class assignment, it was an actual real-world business product. I have a fairly complex application system - so complex that one team of potential outsourcers actually ran away in fear rather than attempt to work with it. And I had the requirement that I should add an XML serialization in order to provide a synchronization mechanism to a third-party product.

So for the most part I simply added JAXB annotations to the existing persistence records and wrote Apache CXF web service request handlers.

There were a few cases where either the structure or format of the original database wasn't appropriate for the new service (for example, date formatting), in those cases, I augmented the basic annotations with some converter annotations and in cases wherer records simply didn't map right, I used Dozer to convert to/from record formats more suitable for the XML serializer.

We're very happy with the results. The new system is more cleaner and much, much more reliable than the older sync mechanism that it replaced.
 
Alexander Petrov
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sharing the same view. In general I try to minimize the amounts of DTOs in a project to only the places where they are absolutely needed.But this is a classroom assignment and what the reviewer thinks is a factor. And since I have been challenged on several occasions about my viewpoint of DTOs I will choose to go with the Mapper. Sounds to me like none will challenge this opposed to your(our) view which I completely share. I make it as a safeguard measure.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic