JPA. How to implement the connection between the header and the document table?

You must implement the connection between the document header and the document table.
I created two entities the document and document line and configured ACC. connection. I'm interested in whether correct architecture? Or there is a standard variant of implementation of this decision.

public class Document extends BaseEntity {
private Date 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;

July 9th 19 at 13:35
1 answer
July 9th 19 at 13:37
Some explanations and tips:
1. The entity DocumentRow no annotations @Entity and @Table
2. Explicitly specify the table name @Table(name = "documents"), @Table(name = "document_rows").
3. Specify clearly the name of the column @Column(name = "date") etc. otherwise one day will rename a field in a class or a column in the database and hibernate will not be able to link the data(or worse, add a new column yourself).
4. @ManyToMany change to @OneToMany. And indeed it is knowledge from the theory of relational databases and to work with them better to see, because the lack of understanding of the types of links will not properly design the database.
5. Get cascade unless explicitly needed, it slows down hibernate.
6. Obviously for relationships specify fetch = FetchType.LAZY. Try not to use EAGER at all.

Find more questions by tags HibernateJavaJava EE