I have the requirement to store data in two different db tables and both have the same structure. This is because system/app data must stored separately from user data.
I found the following solution which works well:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class CombinedDataEo {
// fields
}
@Entity
@Table(name = "system_data")
public class SystemDataEo extends CombinedDataEo {
}
@Entity
@Table(name = "user_data")
public class UserDataEo extends CombinedDataEo {
}
Both have their own Repository and one for the combined entity to query over both tables.
public interface CombinedDataRepository extends JpaRepository<CombinedDataEo, String> {
}
My problem now is that I want to use projection on some CombinedDataRepository methods and the result should contain a boolean field systemData. I tried a lot but the only thing which worked was to create an open projection with SPEL
public interface CombinedDataProjection {
// other getters
@Value("#{target instanceof T(SystemDataEo)}") boolean isSystemData();
}
I find it a little bit ugly so my question is: Is there a better solution? Maybe Listeners or Preprocessors, or others?