0

In this scenario we have some collections of Tables that may or may not have a column A but the objects all inherit from a class X which specifies that column. Is there anything that allows specifying that column A should be persisted/loaded to if it exists but otherwise ignore the lack of its existence because it's not important to that particular class extending X?

This is similar to JPA - Optional columns however in that case the solution has the subtypes specifying the additional columns. In this case the column definition is in the root class and therefore all subclasses inherit it whether or not their tables contain that column.

And yes I know that I could just change everything to be method rather than field annotated but I'm asking if there's something that can be done that avoids this. If not that is also an answer.

Community
  • 1
  • 1
cyborg
  • 5,638
  • 1
  • 19
  • 25
  • You could try overriding the field's getter in the subclass, and annotating it with `@Transient`. – Robby Cornelissen Jun 30 '14 at 08:07
  • As per http://stackoverflow.com/questions/942035/hibernate-jpa-annotating-bean-methods-vs-fields it's not possible to mix and match; I had already tried this and failed, it just doesn't care about methods if you've annotated a field. – cyborg Jun 30 '14 at 08:31

1 Answers1

0

Can't comment, rep too low.

Is there a reason why you must field access? Because property access is considered better from what I know. To be honest, I can't remember last time I used field access :)

Some links:

Property access has a lot more usefulness from what I see.

Community
  • 1
  • 1
ptrdom
  • 100
  • 9
  • "Is there a reason why you must field access?", No but that's the way it is. The point of the question is asking if there's something other than simply updating everything to use property access. It wasn't my choice to do this. – cyborg Jul 04 '14 at 12:05
  • I'd suggest just copy/pasting annotations to methods and using property access. – ptrdom Jul 04 '14 at 13:20
  • Right, yes, I know the obvious change of "change everything to not use fields" exists. I am asking if there is a alternative that is *NOT* this. – cyborg Jul 04 '14 at 14:28
  • Although I'm not Java Guru, in my opinion there is no alternative. – ptrdom Jul 07 '14 at 06:00