Contrary to the comments of @JB Nizet, I really can think of many reasons why we allowed trigger triggering identifiers: executing stored procedures, manually executing SQL queries and executing custom queries in Hibernate, to name a few.
I personally found the following solution quite satisfactory. It allows Hibernate to find the maximum identifier and increment it each time the insert statement is called. But when the operator enters the database, the identifier is ignored and redefined by the generated trigger, so there is no uniqueness in the cluster problem
@Id @GeneratedValue(generator="increment") @GenericGenerator(name="increment", strategy = "increment") private Long id;
The biggest flaw - @GenericGenerator is the Hibernate annotation, so you lose portability of the JPA. It is also not clear to programmers that this identifier is actually associated with a sequence.
Another alternative is to change the trigger only to increase the sequence when the identifier is NULL. See " Hibernation issue with Oracle Trigger to generate an identifier from a sequence ." In most cases, this is the best solution, because it clearly shows that the identifier is associated with a sequence. My only problem is that it gives the user / sleeper the ability to insert any identifier, without actually requesting the sequence in the first place.
Christopher yang
source share