The first is about identity generator, this is something I ingored and find really interesting later when I read back the document.
When you choose "native" (for plain
hibernate) or "AUTO" (for JPA which use
hibernate as provider) as identity generator, it (actually they are the same identity generator but with different name in different scope) will pick other identity generators like identity, sequence, or hilo, depending on the capabilities of the underlying database. Use this generator to keep your mapping metadata portable to different database management systems.
If your underlying DB is oracle, it will automatically create a sequence for you which means choose the sequence as identity generator, that's really interesting, at least for me for the first time when it comes to me.
The second is about the flush and close session operations when to use hibernate with JTA transaction.
You should manually flush your operation by call flush method and close your session by calling close method when you try to use JTA transaction instead of
hibernate Transaction API, these is because
hibernate Transaction API does thoes operations automatically for you. But, with following configuration, it can also be done automatically for you by
hibernate, though you are still with JTA transaction.
hibernate.transaction.flush_before_completion=true
hibernate.transaction.auto_close_session=true
The third is about an interesting code snatch listed as follow:
1 Session session = sessionFactory.openSession();
2 session.beginTransaction();
3 session.save(new Item());
4 session.close();
What happen if you don't commit the transaction? It's depends on the underlying DB, for oracle it will commit any uncommited transactions, but for many other DB vendors, they will roll back any pending transactions.