JPA - 可选列 [英] JPA - Optional columns

查看:84
本文介绍了JPA - 可选列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在每个客户的独立数据库中有许多客户数据,它们应该具有相同的模式/表结构。然而,有一个表在某些数据库中与其他列相比具有额外的列。



例如,对于客户A,表X具有列a,b,c,d 。对于顾客B,有一个表X,列a,c,d。我需要捕获b,如果它存在,但可以忽略它,如果不存在。



有没有办法告诉JPA忽略那些列,如果它们不存在? @Basic(optional = true)完全像我想要的那样,但文档表明它是用于其他目的的。



目前我如预期的那样得到'字段列表'中的未知列'table.field'



PS
我不能只将列添加到没有它们的数据库中。

解决方案

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

我想到的问题可能的解决方案是使用类层次结构,使用 @MappedSuperclass 而不是 @Entity ,然后为每个数据库定义每个具体类。



使用 @MappedSuperclass JPA实现不会寻找一个表来匹配这些字段,所以你甚至可能有一些空的实体类(扩展超类)定义你的模型。


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.

解决方案

@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.

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.

这篇关于JPA - 可选列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆