# While trying to add rows to a table using Hibernate, NonUniqueObjectException is thrown. Why?

**URL:** <https://discourse.hibernate.org/t/while-trying-to-add-rows-to-a-table-using-hibernate-nonuniqueobjectexception-is-thrown-why/7344>\
**Category:** Hibernate ORM\
**Created:** [March 6, 2023, 4:13pm UTC](https://discourse.hibernate.org/t/while-trying-to-add-rows-to-a-table-using-hibernate-nonuniqueobjectexception-is-thrown-why/7344 "2023-03-06T16:13:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nad.Chel](https://avatars.discourse-cdn.com/v4/letter/n/3be4f8/32.png) [@Nad.Chel](https://discourse.hibernate.org/u/Nad.Chel)\
**Post date:** [March 6, 2023, 4:13pm UTC](https://discourse.hibernate.org/t/while-trying-to-add-rows-to-a-table-using-hibernate-nonuniqueobjectexception-is-thrown-why/7344/1 "2023-03-06T16:13:17Z")

</div>

Earlier, I successfully added an annotated class instance to a database table using the method `save()`. At some point, I truncated the table with the help of a MySQL console inside my IntelliJ IDEA (the table is empty). Now, when I try to add a row (or two), I get this exception

```auto
17:55:07.405 [main] ERROR org.hibernate.orm.connections.pooling - Connection leak detected: there are 1 unclosed connections upon shutting down pool jdbc:mysql://localhost:3306/my_db?useSSL=false&serverTimezone=UTC
17:55:07.418 [main] ERROR org.hibernate.orm.connections.pooling - Connection leak detected: there are 1 unclosed connections
Exception in thread "main" org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [org.example.Employee#0]
	at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:179)
	at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:134)
	at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:197)
	at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
	at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:182)
	at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:28)
	at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:78)
	at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:107)
	at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:649)
	at org.hibernate.internal.SessionImpl.save(SessionImpl.java:641)
	at org.hibernate.internal.SessionImpl.save(SessionImpl.java:636)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:345)
	at jdk.proxy2/jdk.proxy2.$Proxy50.save(Unknown Source)
	at org.example.Test.main(Test.java:20)

```

Here’s my main method

```auto
    public static void main(String[] args) {
        try (SessionFactory sessionFactory = new Configuration()
                .configure("hibernate.cfg.xml")
                .addAnnotatedClass(Employee.class)
                .buildSessionFactory()) {
            Session session = sessionFactory.openSession();
            Employee robotnik = new Employee("Ivo", "Robotnik",
                    "Engineering Department", 1_000_000);
// Employee someoneElse = new Employee("Someone", "Else", "Some Department", 1000);
            session.beginTransaction();
            session.persist(robotnik);
// session.persist(someoneElse);
            session.getTransaction().commit();
        }
    }

```

Here’s my entity class

```auto
@Entity
@Table(name="employees")
public class Employee {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(unique = true, nullable = false)
    private int id;
    @Column
    private String name;
    @Column
    private String surname;
    @Column
    private String department;
    @Column
    private int salary;
[...]

```

Could you please tell me why it’s happening, and how I can solve this problem?

Here’s the [repo](https://github.com/NadChel/sql_project/) in case you need to check out the entire code

---

<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:** [March 7, 2023, 9:03am UTC](https://discourse.hibernate.org/t/while-trying-to-add-rows-to-a-table-using-hibernate-nonuniqueobjectexception-is-thrown-why/7344/2 "2023-03-07T09:03:01Z")

</div>

It looks like the model you posted here does not match the model that you are using. The code line in the stack trace should never be hit when you use `GenerationType.IDENTITY`, as the `persister.getGenerator().generate()` method call should return the `IdentifierGeneratorHelper.POST_INSERT_INDICATOR`, which would then run into a different code path.

---

<div class="post-metadata">

**Author:** ![Nad.Chel](https://avatars.discourse-cdn.com/v4/letter/n/3be4f8/32.png) [@Nad.Chel](https://discourse.hibernate.org/u/Nad.Chel)\
**Post date:** [March 7, 2023, 8:05pm UTC](https://discourse.hibernate.org/t/while-trying-to-add-rows-to-a-table-using-hibernate-nonuniqueobjectexception-is-thrown-why/7344/3 "2023-03-07T20:05:19Z")

</div>

It does match

Anyway, I deleted a database and created a new one, cleaned, installed the project via Maven and the problem went away

I got a new one, though. I’ll post it separately
