SSH [SSH] Hibernate framework of: Hibernate primary key generation strategy

Primary key:

NATURAL primary key : a field in a table with meaningful (corresponding to a persistent class of meaningful attributes) as a primary key, for example, a person table is created, the person with the ID number as the key.
Agent primary key : the table is not a meaningful field, its presence is only used to uniquely identify each record. In setting the primary key is usually the case when a surrogate primary key, because nature is increasingly a meaningful field, there may be changes, but a surrogate primary key has no real meaning, subsequent changes do not exist.

Hibernate proxy primary key generation strategy:

In the actual development generally does not allow the user to set the primary key, but automatically generated by the program, mainly provided by Hibernate primary key generation strategy are the following:

  • INCREMENT : suitable for short, int, long types of master key implementation principle is to first send a sql statement to query table greatest id, the id + 1 and as the id of the new record. In principle we can see there is a thread safety problem with this approach, because if multiple threads simultaneously, yet new record into a table, a new thread to query not increase the id, the thread will be generated at this time and on a same thread id, id conflict will occur. Therefore, this method should be used in a single thread.
  • the Identity : suitable for short, int, long types of primary keys, using automatic growth mechanism underlying database, so there is no thread-safety issues. However, it should be noted, DB2, MySQL, MS SQL Server , Sybase and other databases HypersonicSQL automatic growth mechanism, while Oracle does not.
  • Sequence : suitable for short, int, long types of primary key, DB2, PostgreSQL, Oracle, SAP DB, McKoi and other databases using sequence (sequence), but MySQL does not support sequences, you can not use sequence.
  • UUID : primary key of type String applicable, after use the primary key generation strategy, Hibernate automatically generates a random string as a primary key.
  • Native : local policy, local MySQL database is used by the identity strategy, using a local Oracle database using sequence strategy.
  • Assigned : If you use this strategy, then hibernate will give up the management of the primary key is generated, the application to assign an identifier before calling save () for the object. This is the default strategy generated when the <generator> element is not specified. At this point you need to generate your own or be set by programming.
  • Foreign : Use a further object identifier associated. It is usually <one-to-one> used together.
Published 128 original articles · won praise 17 · views 2727

Guess you like

Origin blog.csdn.net/qq_43705275/article/details/104224394