我试着用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只是不支持这个,还是我使用了错误的注释,还是还发生了其他事情?
发布于 2017-12-06 22:24:14
问题的根源似乎在协会的另一边。我本来有这个
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;
}
}https://stackoverflow.com/questions/47065031
复制相似问题