Hibernate custom primary key policy,
In the latest project, use the guid generation policy of hibernate. The configuration is as follows:
<Id name = "id" type = "java. lang. String">
<Column name = "ID" length = "32"/>
<Generator class = "guid"/>
</Id>
We found that the generated IDs are quite regular. First, we can see that the guid of the source hibernate uses the guid algorithm mechanism at the bottom layer of the database, which corresponds to the uuid () function of MYSQL and SQL
Server's newid () function, ORACLE's rawtohex (sys_guid () function, and so on.
Specifically, oracle uses (select rawtohex (sys_guid () from dual)
Use the following pl/SQL test:
Declare
V varchar2 (32 );
Begin
For I in 0 .. 10 loop
Select rawtohex (sys_guid () into v from dual;
Dbms_output.put_line (v );
End loop;
End;
Result:
A36190EE69DDAAE7E040200A8A096CE5
A36190EE69DEAAE7E040200A8A096CE5
A36190EE69DFAAE7E040200A8A096CE5
A36190EE69E0AAE7E040200A8A096CE5
A36190EE69E1AAE7E040200A8A096CE5
A36190EE69E2AAE7E040200A8A096CE5
A36190EE69E3AAE7E040200A8A096CE5
A36190EE69E4AAE7E040200A8A096CE5
A36190EE69E5AAE7E040200A8A096CE5
A36190EE69E6AAE7E040200A8A096CE5
A36190EE69E7AAE7E040200A8A096CE5
Only one character in the middle is different.
The following uses the hibernate custom id policy interface IdentifierGenerator
For the sake of simplicity, use the uuid method that comes with jdk:
Public class UUIDGenerator implements IdentifierGenerator {
Private static Logger logger = Logger. getLogger (UUIDGenerator. class );
Public Serializable generate (SessionImplementor session, Object object)
Throws HibernateException {
String id = UUID. randomUUID (). toString (). replaceAll ("-", ""). toUpperCase ();
Return id;
}
}
Use the following configuration during Configuration:
<Id name = "id" type = "java. lang. String">
<Column name = "ID" length = "32"/>
<Generator class = "com. XXX. XXX. UUIDGenerator"/>
</Id>
~ End ~