最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

spring - Inheritance TABE_PER_CLASS with projection - Stack Overflow

programmeradmin2浏览0评论

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?

发布评论

评论列表(0)

  1. 暂无评论