In our project we use JPA with Hibernate implementation. Here are our entities classes:
@Entity
@Table(name = "t_apples")
public class Apple {
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "tree_id", nullable = false)
private Tree tree;
@Column(name = "color")
private int color;
//...
}
@Entity
@Table(name = "t_trees")
public class Tree {
@Id
@Column(name = "tree_id")
private Long id;
@Column(name = "height")
private int height()
@Transient
private Set<Apples> apples;
//...
}
Lets say we have the following function.
void createTree() {
EntityTransaction transaction = manager.getTransaction();
transaction.begin();
Tree tree = new Tree();
tree.setHeight(10);
entityManager.persist(tree);
entityManager.flush();
for (int i = 0; i < 5; i++) {
Apple apple = new Apple(COLORS[i]);
apple.setTree(tree);
entityManager.persist(apple);
}
transaction.commit();
}
The question is: Do we really need to use flush() method here? Why or why not? I couldn't find anything about this in documentation.