2

我有以下实体:

@Entity
public class Owner{
@Id
@Column(name = "OWNER_ID")

    @OneToMany()
    @JoinColumn(name = "OWNER_ID")
    private Set<Parent> parents;
    ...
}

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Parent{
    @Id
    @Column(name = "PARENT_ID")
    ...
}

@Entity
public class ChildA extends Parent{
    ...
}

@Entity
public class ChildB extends Parent{
    ...
}

}

问题是当我尝试使用父元素持久化 Owner 时出现以下异常:

org.springframework.dao.InvalidDataAccessResourceUsageException:无法插入集合:[sample.Owner.parents#1];SQL [更新父集OWNER_ID=? 其中 PARENT_ID=?]; org.springframework.dao.InvalidDataAccessResourceUsageException:无法插入集合:[sample.Owner.parents#1];SQL [更新父集OWNER_ID=? 其中 PARENT_ID=?]; 嵌套异常是 org.hibernate.exception.SQLGrammarException:无法插入集合:[sample.Owner.parents#1] 嵌套异常是 org.hibernate.exception.SQLGrammarException:无法插入集合:[sample.Owner.parents#1]

如果我将 InheritanceType 更改为 JOINED 它工作正常。关于为什么它试图坚持父抽象类而不是子特定类的任何想法?

4

1 回答 1

2

Hibernate 要求与每类表层次结构的多态关联是双向的(2.2.4.1. 每类表):

public class Owner {
    @OneToMany(mappedBy = "owner")
    private Set<Parent> parents;
    ...
}

public clas Parent {
    ...
    @ManyToOne
    @JoinColumn(name = "OWNER_ID")
    private Owner owner;
    ...
}
于 2012-04-09T12:22:31.643 回答