Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

java - JPA - Optional columns

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...