Summarize the points to note when using JPA

Source: Internet
Author: User
  • 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.