• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA optional field

 
Alaneus Kingingestingest
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use JPA (Toplink) and was wondering if there is such thing as optional field.

Let me explain what I mean:

I'm writing application that can connect to different databases.
For example let's say that in all databases there is table "products", but some have field "quantity", and some not.
So I was wondering if I can have one entity class Product which has all fields mapped (including "quantity"),
but does not generate error while fetching if some database table lacks of that field.
Instead that attribute in object is set to default value (null).

If JPA does not allow such behavior, is there good alternative solution?
 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No there is no such feature.

You could have a different persistence unit for the databases without the field, or use a SessionCustomizer or SessionEvent do customize your mappings when starting up to remove the mapping using the Descriptor and Mapping code API.

You could also define a view on your database and map to the view which can use null for the missing field.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic