【问题标题】:@Where with @SecondaryTable does not work with Hibernate@Where with @SecondaryTable 不适用于 Hibernate
【发布时间】:2019-02-06 11:08:09
【问题描述】:

我的模型中存在一对多关系,其中子实体存储在两个表中。

@Entity
@Table(name = "child1")
@SecondaryTable(name = "child2", pkJoinColumns = {
        @PrimaryKeyJoinColumn(name = "id1", referencedColumnName = "id1"),
        @PrimaryKeyJoinColumn(name = "id2", referencedColumnName = "id2")})
@Where(clause = "col1 is not null and col2 is not null")
@Data
@Immutable
public class Child implements Serializable {...}

Child 实体与Parent 实体一起被急切地获取。问题出在@Where 子句中,它应该引用两个表中的列:col1 在表child1 中,col2child2 中。这会引发以下错误:

ERROR 12333 --- [nio-8183-exec-7] o.h.engine.jdbc.spi.SqlExceptionHelper   : Column (col2) not found in any table in the query (or SLV is undefined).
java.sql.SQLException: null
...
  1. 仅使用:@Where(clause = "col1 is not null") 提供正确的映射并且不会产生错误。
  2. 使用@Where(clause = "child1.col1 is not null and child2.col2 is not null") 会出现以下错误:

    Column (child1) not found in any table in the query (or SLV is undefined).
    

我怎样才能让@Where 使用两个表或有任何解决方法?

不过有一些要求:

  1. 我使用 informix 作为底层数据库,并且具有只读访问权限。
  2. 我知道,它可以通过原生 SQL 甚至 JPQL/criteria API 等来解决,但是这样做会让我重写很多核心。我想避免它。

【问题讨论】:

    标签: java sql hibernate jpa informix


    【解决方案1】:

    这是由于HHH-4246 问题造成的。

    一种解决方法是使用@MapsId@SecondaryTable 替换为@OneToOne 关联。

    这样,child2 表变成了Child2 实体,您可以对其使用@Where@Filter

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-30
      • 2017-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-09
      相关资源
      最近更新 更多