This is my current solution:
Code:
Session session = HibernateSessionFactory.getSession();
Item item = new Item();
Integer itemNo = (Integer) session.createSQLQuery("select NextID from temp").list().get(0);
String year = "07"; // TODO: hardcoded
String id = year + String.format("%06d", itemNo);
while (session.createSQLQuery("select ID from item where ID = '" + id + "'").list().size() > 0)
id = "07" + String.format("%06d", ++itemNo);
item.setId(id);
session.save(item);
session.createSQLQuery("update temp set NextID = " + ++itemNo).executeUpdate();
session.flush();
session.close();
please comment on it!