Hibernate: Parent/Child relationship for a single table with composite key
A similiar topic has already been covered, but without the problem of having a composite key in the table. Basically I am trying to get a hierarchy tree out of a single table, which has the following structure:
- Four columns representing the composite key
- A column containing the parent Id
- Other columns
My objective is to map a single class to this table using hibernate annotations and then build my hierarchy tree out of it:
@Entity
@Table(name = "Enti")
public class DevUnit {
/** The valuable fields from the Ent开发者_如何学编程i table */
// Primary Key (PK) Object
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "stazione", column = @Column(name="IdStazione")),
@AttributeOverride(name = "categoria", column = @Column(name="IdCategoria")),
@AttributeOverride(name = "tipoente", column = @Column(name="IdTipoEnte")),
@AttributeOverride(name = "ente", column = @Column(name="IdEnte"))
})
private EnteDBPK EnteDBPK;
@OneToMany
@JoinColumn(name = "parentId")
private List<DevUnit> children = new ArrayList<DevUnit>();
@ManyToOne(targetEntity = DevUnit.class, optional=true, fetch=FetchType.LAZY)
@JoinColumn(name = "parenId",insertable=false,updatable=false)
private DevUnit parent = null;
// Getters and setters...
}
The EnteDBPK is my composite key, defined as:
@Embeddable
public class EnteDBPK implements Serializable {
private static final long serialVersionUID = 2960251547408481498L;
/** Fields representing the composite primary key*/
private long stazione;
private long categoria;
private long tipoente;
private long ente;
public EnteDBPK(){}
public EnteDBPK(long stazione, long categoria, long tipoente, long ente){
this.stazione = stazione;
this.categoria = categoria;
this.tipoente = tipoente;
this.ente = ente;
}
// Getters and setters, equals and hashcode overrides...
}
The associations are probably not correctly defined, as I get this error when I try to run it:
org.hibernate.AnnotationException: A Foreign key refering package.DevUnit from package.DevUnit has the wrong number of column. should be 4
I'm sure I'm missing something in both the associations, but I need your help to understand the problem and find a solution to it.
Thanks for your help.
Antonio
If composite key of DevUnit
consists of 4 columns, foreign key that references it should consist of 4 columns as well:
@OneToMany(mappedBy = "parent")
private List<DevUnit> children = new ArrayList<DevUnit>();
@ManyToOne(optional=true, fetch=FetchType.LAZY)
@JoinColumns({
@JoinColumn(name = "parentIdStazione", referencedColumnName = "IdStazione"),
@JoinColumn(name = "parentIdCategoria", referencedColumnName = "IdCategoria"),
@JoinColumn(name = "parentIdTipoEnte", referencedColumnName = "IdTipoEnte"),
@JoinColumn(name = "parentIdEnte", referencedColumnName = "IdEnte")
})
private DevUnit parent = null;
Actually you can omit @JoinColumns
here if default names are fine.
Also note that your approach to mapping bidirectional one-to-many/many-to-one relationship (with "one" side being the owning side) is not recommended by Hibernate documentation, so that I used another approach in the snippet above, see 2.2.5.3.1.1. Bidirectional.
精彩评论