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.
So, you can wrap the optional attribute into an Optional instead of returning it directly. But please be aware, that this doesn't include any lazy loading and just wraps the already selected value of the database column into an Optional.
Optional is a container object which may or may not contain a non-null value. You must import java. util package to use this class. If a value is present, isPresent() will return true and get() will return the value.
@Column. Let's start with the @Column annotation. It is an optional annotation that enables you to customize the mapping between the entity attribute and the database column.
179: @Basic(optional = false) @Column(nullable = false) The @Basic annotation marks the property as not optional on the Java object level. The second setting, nullable = false on the column mapping, is only responsible for the generation of a NOT NULL database constraint.
@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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With