Dimitrios Douras
Dimitrios Douras

Reputation: 506

Hibernate - Is including Set and OneToMany annotation in parent Entity necessary?

I have the below parent entity

@Entity
@Table(name = "EntityA", catalog = "mycatalog")
public class EntityA implements java.io.Serializable {

private int parentId;
private String a;
    private Set<EntityB> entityBs = new HashSet<entityB>(0);

public EntityA() {
}

public EntityA(int parentId) {
    this.parentId = parentId;
}

public EntityA(int parentId, String a) {
    this.parentId = parentId;
    this.a = a;
}

@Id
@Column(name = "PARENT_ID", unique = true, nullable = false)
public int getParentId() {
    return this.parentId;
}

public void setParentId(int parentId) {
    this.parentId = parentId;
}

@Column(name = "columnA", length = 64)
public String getA() {
    return this.a;
}

public void setA(String a) {
    this.a = a;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "EntityA")
public Set<EntityB> getEntityBs() {
    return this.entityBs;
}

public void setEntityBs(Set<EntityB> entityBs) {
    this.entityBs = entityBs;
}

and the below child entity

@Entity
@Table(name = "EntityB", catalog = "mycatalog")
public class EntityB implements java.io.Serializable {

private int childId;
private EntityA entityA;
private String b;


public EntityB() {
}

public EntityB(int childId, EntityA entityA) {
    this.eventId = eventId;
    this.entityA = entityA;
}

public EntityB(int childId, EntityA entityA, String b) {
    this.childId = childId;
    this.entityA = entityA;
    this.b = b;
}

@Id
@Column(name = "CHILD_ID", unique = true, nullable = false)
public int getChildId() {
    return this.childId;
}

public void setChildId(int childId) {
    this.childId = childId;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "PARENT_ID", nullable = false)
public EntityA getEntityA() {
    return this.entityA;
}

public void setEntityA(EntityA entityA) {
    this.entityA = entityA;
}

@Column(name = "columnB", length = 64)
public String getB() {
    return this.b;
}

public void setB(String b) {
    this.b = b;
}
}

I have noticed that querying those entities work even if I do not include the Set and the OneToMany annotation in the parent table. See below parent without the Set:

@Entity
@Table(name = "EntityA", catalog = "mycatalog")
public class EntityA implements java.io.Serializable {

private int parentId;
private String a;


public EntityA() {
}

public EntityA(int parentId) {
    this.parentId = parentId;
}

public EntityA(int parentId, String a) {
    this.parentId = parentId;
    this.a = a;
}

@Id
@Column(name = "PARENT_ID", unique = true, nullable = false)
public int getParentId() {
    return this.parentId;
}

public void setParentId(int parentId) {
    this.parentId = parentId;
}

@Column(name = "columnA", length = 64)
public String getA() {
    return this.a;
}

public void setA(String a) {
    this.a = a;
}

}

Can you please explain the difference and if and when it is necessary to include the Set in the parent table?

Upvotes: 1

Views: 185

Answers (1)

kostja
kostja

Reputation: 61548

Your parent entity has a one-to-many relationship to it's children.

The relationship is is mapped by a join column, and this column is always in the table of the many-side, which is therefore called the owning side. So technically, the relationship is a unidirectional one, from the children to their parent.

The table for the parent entity does not contain any relationship information and it is therefore called the non-owning or inverse side. JPA implementations can 'emulate' a bidirectional relationship if a properly annotated children field is added on the parent side, but it is not necessary.

If you need to traverse from a parent to it's children, you can add the field. If you only traverse from the children to the parent, do not add it.

You can notice that the non-owning side must tell the persistence provider the attribute name, by which it is referenced on the owning side, using the mappedBy attribute, so the relationship can work bidirectionally on the object level.

Upvotes: 5

Related Questions