• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA and Decorator pattern

 
Jason Nerothin
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to implement the decorator pattern to solve a design problem and don't know how best to work it with Toplink...

I have a number of JAXB-generated Java code that I'm using to get started. That means that there's a bunch of imported javax.xml.datatype.XMLGregorianCalendars lying around that aren't Serializable and are otherwise cantankerous since they extend (just) Object.

My problem is this:

For any given concrete jaxb class (say ClazzJaxb) I'd like to extract an interface IClazz containing (only) easily-persistable types, have it implemented by an Abstract class ClazzAbst containing GregorianCalendar analogues to the original ClazzJaxb's XMLGregorianCalendar accessor methods, and then (finally) implement a concrete subclass ClazzActual of ClazzAbst that delegates all simple accessors directly to a decorated instance of ClazzJaxb using the decorator pattern and then deals with the problem cases separately. (mouthful, I know)

So far, so good. But when I get back to thinking about JPA, I need actual attributes in my ClazzActual class so that I can mark my decorated instance @Transient and get on with life. Doing so, however, breaks the decorator pattern completely and ends up doubling the work: I need to make each setter and getter update both the local copy and the contained copy.

Do I give up and go to a static factory method (or ... ?) at instantiation to turn ClazzJaxbs into persistable ClazzActuals? Seems to diminish the value of the original code-gen...



JPN
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, my only issue that I see is that you are starting with generated code, then changing that code. So what happens if you have to regenerate your code. How do you get your changes into that newly generated code?

If you create your JAXB code from hand, or let JAXB generate it the first time, then change it by removing the offending Object type, do that and make it work how you would like it to be.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic