Search code examples
hibernatejpamany-to-onemappedsuperclass

JPA relation between two classes


I have a task to build a document which contains header and rows. for example Stock income document which contains the header (date, stock) and the rows (material, quantity, price, sum). My problem is that I am not sure that my classs architecture is right. Code is here (JPA + Hibernate):

@Entity
@Table
public class Document extends BaseEntity {
@Column
@Temporal(TemporalType.DATE) 
private Date date;
@Column
@Temporal(TemporalType.DATE) 
private Date createDate;
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Set<DocumentRow> rows;
...
}

public class DocumentRow extends BaseEntity {
@ManyToOne(optional = false, cascade = CascadeType.ALL)
private Document document;
@Column(nullable = false, unique = true)
private Integer row;
...
}

@MappedSuperclass
public abstract class BaseEntity implements Serializable {

private static final long serialVersionUID = 8171308450531596347L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; ...}

Solution

  • You are doing a bidirectional mapping and you can't combine @ManyToOne and @ManyToMany, despite the logic you are applying, the sql isn't correct because :

    @ManyToMany will result in an association Table ManyToOne just a foreign key to the parent entity on the other entity.

    If a DocumentRow element can be shared between two different Document then you need a ManyToMany relationship which will result in creating a joinTable and you have to change the @ManyToOne on DocumentRow Table if you want a bidirectional relationship :

    @ManyToMany
    private List<Document> documents;
    

    If it is not the case and a DocumentRow belongs to just one document, then you need to change the mapping on the parent class which is Document from @ManyToMany to @OneToMany this way :

    @Entity
    @Table
    public class Document extends BaseEntity {
    @Column
    @Temporal(TemporalType.DATE) 
    private Date date;
    @Column
    @Temporal(TemporalType.DATE) 
    private Date createDate;
    @OneToMany(mappedBy="document",...)
    private Set<DocumentRow> rows;
    ...
    }