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.

4

1 回答 1

14

@Basic(optional=true)它只是告诉模式生成器(如果有)该字段可以包含空值,而不是该字段可能存在或不存在。

我想到的一个可能的解决方案是使用类层次结构,定义一个公共父类,@MappedSuperclass而不是,@Entity然后为从该数据库扩展的每个数据库定义每个具体类。

使用@MappedSuperclassJPA 实现不会寻找与这些字段匹配的表,因此您甚至可能有一些空实体类(扩展超类)来定义您的模型。

于 2013-09-30T16:10:29.190 回答