A
A
AstronautX2016-05-17 11:31:55
Java
AstronautX, 2016-05-17 11:31:55

JPA. How to implement a link between the header and table of a document?

It is necessary to implement a link between the document header and the document table.
I created two entities the document and a line of the document and adjusted . connections. Whether me the architecture is correct? Or there is some standard implementation of this solution.

@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;

...
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vayho, 2016-05-17
@vayho

A few explanations and tips:
1. The DocumentRow entity does not have @Entity and @Table annotations
2. Specify the table name explicitly @Table(name = "documents"), @Table(name = "document_rows").
3. Specify explicitly the name of the columns @Column(name = "date"), etc. otherwise, once you rename a field in a class or a column in a database, hibernate will not be able to link the data (or even worse, it will add a new column on its own).
4. Change @ManyToMany to @OneToMany. And in general, this knowledge is from the theory of relational databases, and it is better to familiarize yourself with them before working, because a lack of understanding of the types of relationships will not allow you to design a database normally.
5. Remove the cascade if it is clearly not needed, it slows down the hibernate.
6. Specify fetch = FetchType.LAZY explicitly for links. Try not to use EAGER at all.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question