9

We have many customers data in separate databases per customer which should have the same schema/table structures. However there is a table that has extra columns in some databases compared to others.

For example for customer A there is a table X with columns a, b, c, d. For customer B there is a table X with columns a, c, d. I need to capture b if it exists but can ignore it if not.

Is there a way to tell JPA to ignore those columns if they don't exist? @Basic(optional=true) reads exactly like what I want but the documentation indicates it is for another purpose.

Currently I get, as expected, Unknown column 'table.field' in 'field list'

P.S. I can't just add the columns to the databases that don't have them unfortunately.

Josh
  • 2,842
  • 8
  • 45
  • 51
  • 1
    `@Basic(optional=true)` it's just to tell the schema generator (if any) that the field can hold `null` values, no that the field might or not be present. A possible solution for your problem that comes to my mind is to use a class hierarchy, defining a common parent class with `@MappedSuperclass` instead of `@Entity` and then defining each concrete class for each database extending from that one. – Alonso Dominguez Sep 30 '13 at 15:59
  • just realised that my comment could be good enough to be an answer!! – Alonso Dominguez Sep 30 '13 at 16:07

1 Answers1

14

@Basic(optional=true) it's just to tell the schema generator (if any) that the field can hold null values, not that the field might or might not be present.

A possible solution for your problem that comes to my mind is to use a class hierarchy, defining a common parent class with @MappedSuperclass instead of @Entity and then defining each concrete class for each database extending from that one.

With @MappedSuperclass the JPA implementation wouldn't look for a table to match those fields so you might even have some empty entity classes (extending the super class) just to define your model.

Alonso Dominguez
  • 7,750
  • 1
  • 27
  • 37
  • I'm not sure how that would work. What would the @Inhertiance strategy be? They all have the same table names and I can't add a column to distinguish the types. The only way they differ is the lack of a column or two. – Josh Sep 30 '13 at 16:26
  • @Josh that's the point of the `@MappedSuperclass` annotation, it doesn't map to any table, it's just used as an abstraction for entities that hold the same set of attributes. So there is no need for a `@Inheritance` annotation. In the other hand, if your entities map to the same table name they can still have different class names, just override the table name with the `@Table` annotation. – Alonso Dominguez Sep 30 '13 at 17:03
  • Ah I see now. I've never used it like that before. That appears to work. Thanks! – Josh Sep 30 '13 at 17:33
  • Thanks for this information - "With @MappedSuperclass the JPA implementation wouldn't look for a table to match those fields " – shakhawat Dec 31 '15 at 05:53
  • @Josh I know it is a long time. But how did the above solution work for you? When I try to do the same the fields are inherited in the child class and I get the error as the Column name is not returned from the DB. – AbhishekB Jul 19 '19 at 13:08
  • 1
    @AbhishekB it's been years since I don't use Hibernate but when I gave this answer, that was the point of it, that the fields would be inherited but that it would not try to lookup for a DB table *named* as the `MappedSupperclass`, but will look for the columns that represent the fields in the table for the `Entity`. Best piece of advice I can give you right now is: do not use Hibernate, you don't really need it. – Alonso Dominguez Jul 19 '19 at 14:40
  • @AlonsoDominguez The project that I work on uses hibernate which works fine except this one stored procedure which has 2 different outputs based on the input. Some columns are missing in the second output. – AbhishekB Jul 22 '19 at 09:25