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

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

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

    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

@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 in case you need to check out the entire code

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.

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