I like to persist an entity and want to select it from the database. If I do this with mysql, it is working, if I do it in Oracle it is not working. So I have the following Code:
EntityManager em = emf.createEntityManager();
em.setFlushMode(FlushModeType.COMMIT);
em.getTransaction().begin();
Anrede anrede = new Anrede();
anrede.setBezeichnung("Test");
System.out.println("persist");
em.persist(anrede);
System.out.println("anrede id: " + anrede.getId());
List<Anrede> anreden = em.createQuery("from Anrede a where a.bezeichnung='Test'").getResultList();
if (anreden.isEmpty())
System.out.println("Notfound");
for (Anrede anrede1 : anreden) {
System.out.println("found: " + anrede1 + " " + anrede1.getBezeichnung());
}
If I run the code with MySQL, I get the follwing output:
persist
Hibernate: insert into anrede (version, bezeichnung) values (?, ?)
anrede id: 26
Hibernate: select anrede0_.id as id1_7_, anrede0_.version as version2_7_, anrede0_.bezeichnung as bezeichn3_7_ from anrede anrede0_ where anrede0_.bezeichnung='Test'
found: Anrede [id=26] Test
If I run the code with Oracle, I get the follwing output:
persist
Hibernate: select hibernate_sequence.nextval from dual
anrede id: 7858215
Hibernate: select anrede0_.id as id1_7_, anrede0_.version as version2_7_, anrede0_.bezeichnung as bezeichnung3_7_ from anrede anrede0_ where anrede0_.bezeichnung='Test'
Notfound
The difference between MySQL and Oracle is that Oracle uses a sequence to generate the ids and MySQL uses column identity. In the output u see that mysql is running the insert query and oracle is not running the insert query, it is only selecting the next ID from the sequece.
If I debug Hibernate I see that MySQL uses the class EntityIdentityInsertAction with the method isEarlyInsert that returns true
and Oracle uses the class EntityInsertAction with the method isEarlyInsert that returns false.
If I change the EntityInsertAction class that the method isEarlyInsert returns true then it works as aspected, but I dont think it is a good Idea to change the hibernate code, that could break other thinks.
Anyway the code shows a different behavior if I’m using differnt Databases. Is it possible to configure Hibernate in Orcale to use “EarlyInserts”?
I could flush the entitymanger or use the FlushModeType.AUTO to have allmost the same behavior, but then I’m getting huge Performance Issues.
I’m using Hibernate 5.6.15.
Can someone help me out?