I have one question regarding primary key generation in Hibernate. I'm working in maintaining existing registry system. Current design use string as a primary key. The rule is something like "EXE" + max()
. Below is how the table look like.
+----------+---------------------------+----------------+
| ID | Email | Name |
+----------+---------------------------+----------------+
|EXE1 | email1@gmail.com | Name 1 |
+----------+---------------------------+----------------+
|EXE5 | email5@gmail.com | Name 5 |
+----------+---------------------------+----------------+
|EXE14 | email14@gmail.com | Name 14 |
+----------+---------------------------+----------------+
|EXE15 | email15@gmail.com | Name 15 |
+----------+---------------------------+----------------+
Currently im using below's code to generate the ID.
Long rowCount = (Long) getSession().createCriteria(Exemption168DB.class).setProjection(Projections.rowCount()).uniqueResult();
if(rowCount == null)
rowCount = 0L;
return String.format("%s%d", CommonConstant.EXEMPTION_KEY_PREFIX, rowCount + 1);
But the problem is; it is using row count to get the next sequence digit. So in the above case, the method will return EXE5
(this ID is already exist in the table thus exception is thrown) because the rowcount in the table is 4, then increment by 1. What I need is EXE16
.
Any help is much appreciated. Extra info, we are using Informix as a database engine.