JPA @ManyToOne @JoinFormula抛出ClassCastException

时间:2017-11-01 23:06:05

标签: java hibernate hibernate-mapping

我正在尝试使用JPA建模多对一的双向关联。联接使用公式。我已经尝试了几种方法,如下所示。一次只使用JoinFormula,另一次使用JoinColumnsOrFormulas。

public class JobOperation
{
  private Operation                 operation;

  @ManyToOne
  //  @JoinFormula("CASE WHEN attribute7 IS NULL OR TO_NUMBER(attribute7) = 0 THEN standard_operation_id ELSE TO_NUMBER(attribute7) END")
  @JoinColumnsOrFormulas(
  {
    @JoinColumnOrFormula(formula = @JoinFormula(//
        value = "(CASE WHEN this_.attribute7 IS NULL OR TO_NUMBER(this_.attribute7) = 0 THEN this_.standard_operation_id ELSE TO_NUMBER(this_.attribute7) END)", // 
        referencedColumnName = "standard_operation_id"))
  })
  @Fetch(FetchMode.SELECT)
  @NotFound(action = NotFoundAction.IGNORE)
  public Operation getOperation()
  {
    return this.operation;
  }
}

我最初使用的是Hibernate 4.3.9,然后尝试使用Hibernate 5.1.0。两者都抛出相同的异常:

15:55:21,408 DEBUG [org.hibernate.cfg.annotations.TableBinder] Retrieving property com.icumed.ifactory3.dto.wip.JobOperation.operation
15:55:21,409 DEBUG [org.hibernate.jpa.HibernatePersistenceProvider] Unable to build entity manager factory
java.lang.ClassCastException: org.hibernate.mapping.Formula cannot be cast to org.hibernate.mapping.Column
    at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:584)

Hibernate的TableBinder类中没有任何内容引用公式。 Hibernate不支持这个,或者我使用错误的注释,还是还有其他事情发生?

1 个答案:

答案 0 :(得分:0)

问题的根本原因似乎是协会的另一方面。我最初有这个

public class Operation extends AbstractOperation
{
  @OneToMany(mappedBy="operation")
  public Set<JobOperation> getJobOperations()
  {
    return this.jobOperations;
  }
}

当我将其更改为以下内容时,它有效。

public class Operation extends AbstractOperation
{
  @OneToMany
  @JoinColumn(name="STANDARD_OPERATION_ID")
  public Set<JobOperation> getJobOperations()
  {
    return this.jobOperations;
  }
}