# Different bahaviour between MySQL and Oracle after persist and select

**URL:** <https://discourse.hibernate.org/t/different-bahaviour-between-mysql-and-oracle-after-persist-and-select/8386>\
**Category:** Hibernate ORM\
**Created:** [October 12, 2023, 9:55am UTC](https://discourse.hibernate.org/t/different-bahaviour-between-mysql-and-oracle-after-persist-and-select/8386 "2023-10-12T09:55:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oconnor](https://avatars.discourse-cdn.com/v4/letter/o/2acd7d/32.png) [@Oconnor](https://discourse.hibernate.org/u/Oconnor)\
**Post date:** [October 12, 2023, 9:55am UTC](https://discourse.hibernate.org/t/different-bahaviour-between-mysql-and-oracle-after-persist-and-select/8386/1 "2023-10-12T09:55:38Z")

</div>

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:

```auto
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:

```auto
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:

```auto
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?

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [October 13, 2023, 12:47pm UTC](https://discourse.hibernate.org/t/different-bahaviour-between-mysql-and-oracle-after-persist-and-select/8386/2 "2023-10-13T12:47:37Z")

</div>

> 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?

Well, you are using non-default settings i.e. `FlushModeType.COMMIT`, so it is expected that there might be such issues. I don’t know what sort of performance issues you think you are getting, but the difference in behavior is because your id generation (`@GeneratedValue`) uses the `AUTO` strategy. On MySQL this will use default to `IDENTITY` and on Oracle to `SEQUENCE`.

You can change the setting to `@GeneratedValue(strategy = IDENTITY)` if you want the same behavior on all databases.
