I have single JPA entity and I would to add self join on this table. Table looks like e.g.
@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;
And IdClass looks like:
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;
As per my business logic I need to add self join only on single column
final Join<TableA, TableA> joinASelf = joinX.join("tableA", JoinType.INNER);
But table has composite primary key, so more than one fields are annotated with @Id. And I get exception like:
Caused by: org.hibernate.AnnotationException: A Foreign key refering com.data.TableA from com.data.TableA has the wrong number of column. should be 2.
How do i add self join only on single column here? I am new to JPA, so please let me know if I missed anything.
Update 21 Feb 2015: I added annotation @AssociationOverride to override associations:
@ManyToOne
@AssociationOverride(name="tableA",
joinColumns=@JoinColumn(name="COLUMN_1"))
private TableA tableA;
Generated column name is shown as "TABLE_A_COLUMN_2". I am not able to find out why. Any clue?