我有一个 JPA 实体,我想在这个表上添加自加入。表看起来像例如
@Entity
@Table(name = "TABLE_A")
@IdClass(TableAPk.class)
public class TableA implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "COLUMN_1", nullable = false, length = 64)
private String column_1;
@Id
@Column(name = "COLUMN_2", nullable = false, precision = 10, scale = 2)
private BigDecimal column_2;
@ManyToOne
@JoinColumn(name = "COLUMN_1", insertable = false, updatable = false)
//@ManyToOne(optional = true, fetch = FetchType.LAZY)
//@JoinTable(name = "KEY_MAPPING",
// joinColumns = { @JoinColumn(name = "J_COLUMN_1", referencedColumnName = "COLUMN_1", insertable = false, updatable = false) } )
private TableA tableA;
@OneToMany(mappedBy="tableA", fetch = FetchType.LAZY)
private Set<TableA> tableASet;
IdClass 看起来像:
public class TableAPk implements Serializable {
// default serial version id, required for serializable classes.
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 1L;
private String column_1;
private BigDecimal column_2;
根据我的业务逻辑,我只需要在单列上添加自加入
final Join<TableA, TableA> joinASelf = joinX.join("tableA", JoinType.INNER);
但是表有复合主键,所以不止一个字段用@Id注解。我得到如下异常:
引起:org.hibernate.AnnotationException:从 com.data.TableA 引用 com.data.TableA 的外键列数错误。应该是2。
我如何在此处仅在单列上添加自加入?我是 JPA 的新手,所以如果我遗漏了什么,请告诉我。
2015 年 2 月 21 日更新:我添加了注释 @AssociationOverride 来覆盖关联:
@ManyToOne
@AssociationOverride(name="tableA",
joinColumns=@JoinColumn(name="COLUMN_1"))
private TableA tableA;
生成的列名显示为“TABLE_A_COLUMN_2”。我无法找出原因。有什么线索吗?