I have done some debugging and I can simplify my question.
Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
C c1 = new C();
c1.setName("c1");
session.saveOrUpdate(c1);// This one need an immediate insert in order to get the Id assigned
session.getTransaction().commit();
But at the end of this transaction, second level cache doesn't have c1 stored.
However If I use assigned primary key as below where the insertion can wait until I call commit, second level cache stores the c1.
Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
C c1 = new C();
c1.setName("c1");
c1.setId(1L);
session.saveOrUpdate(c1);// This call won't trigger an immediate insert
session.getTransaction().commit();