1

I have met a situation that my entity may related to the tables which has different columns.

For example, my entity has 2 properties(A and B). Related tables may have two versions, one table has A column only and the other table has A and B columns. So if using entity in one column table, it will raise exception about the property B

I try to use @Transient to mark B properties. It does not meet exception in one column situation. But in two columns table, the column B's information is missing since I omit B properties.

So, I expected when in one column table, the B properties can be set as null. Also in the two columns table, the B properties value can be caught from DB.

I try to use the @MappedSuperclass to create a base abstract class and create a class extends base class. But no matter where I put B properties, in base class or extended class. Or no matter which I use findAll() method or use @Query(native sql query) to query, it still raise the same exception as before.

When using findAll(): ERROR: column table.B does not exist

When using @Query(native sql query): The column name B was not found in this ResultSet.

Sana
  • 360
  • 3
  • 13
poisson0106
  • 77
  • 1
  • 11
  • Possible duplicate of [JPA - Optional columns](https://stackoverflow.com/questions/19098541/jpa-optional-columns) – Akirus Aug 06 '19 at 08:31
  • If I correctly understand: You map one entity to two different tables? That's not possible with JPA. – Simon Martinelli Aug 06 '19 at 09:12
  • @SimonMartinelli, actually there is one table. But with different version, version 2 has one more column. So the entity has the same property numbers as version 2. But in some situation, the new entity will be used with version 1 table, and we can't change version 1's structure. So we need a method to make new entity also run in version 1 situation. – poisson0106 Aug 06 '19 at 09:16
  • @Akirus, thank you. I have done some try and updated the result in description part. Seems it not work... – poisson0106 Aug 06 '19 at 09:22
  • That's not possible. – Simon Martinelli Aug 06 '19 at 09:25
  • @SimonMartinelli, ok...maybe I need to rethink another solution. Thank you for your help. – poisson0106 Aug 06 '19 at 09:40

0 Answers0