- Before invoking a
persist(entity) method, make sure that entity all non-collection class members are correctly assigned.
- In a
@OneToMany relationship, if you want to delete an entity of the many (active) side, you must first remove the entity from the collection class on the one side (the passive side) before calling the remove(entity) method to remove the success.
- In a
@OneToMany relationship where cascade deletions are not set, if you want to remove the one-side (passive-side) entity, you must first delete all the entities in the one-side collection class (or set the corresponding field for all entities on the many side to null) to delete the one-side entity itself. Otherwise, a data Table foreign key association error is thrown.
- If you use only the primary key value of an entity, or if you call only the entity's
setXX() methods, then using it to find that entity getReference can effectively reduce the number of database queries. See StackOverflow
- When a transaction commits, the
EntityManager entity state is synchronized with the database, typically without manual calls, and flush() no more calls are required to update the entity persist() merge() .
- When you operate on an entity, always be aware of whether it is still within the scope of the transaction.
- If you want to eager the collection classes in an entity, it is a good idea to use statements at query time to
JOIN avoid a database query that fires when you traverse an entity.
- The process of searching for related records in a data table is more time-consuming than copy data from a datasheet to an entity object. Therefore, you should minimize the number of database queries, rather than "save memory" and read only the values of a few fields.
Summarize the points to note when using JPA