BUG: @ManyToOne and cascade saving

Hello,

//...
open class City {
    // ...
    var id: UUID? = null

    @InstanceName
    @Column(name = "NAME")
    var name: String? = null
}
//...
open class ZipCode {
    //...
    var id: UUID? = null

    @JoinColumn(name = "CITY_ID", nullable = false)
    @NotNull
    @ManyToOne(fetch = FetchType.LAZY, optional = false, cascade = [CascadeType.PERSIST, CascadeType.MERGE])
    var city: City? = null
}

Using those entities, I’m trying to create a ZipCode and fill the city field with an EntityComboBox. When I save the the zipCode, the logs say:

Internal Exception: java.sql.SQLIntegrityConstraintViolationException: integrity constraint violation: unique constraint or index violation; PK_CITY table: CITY
Error Code: -104
Call: INSERT INTO CITY (ID, NAME) VALUES (?, ?)
	bind => [042684df-78d0-8206-6ef5-ccd7d1c3610b, okok]
Query: InsertObjectQuery(com.company.buginsert.entity.City-042684df-78d0-8206-6ef5-ccd7d1c3610b [managed])

Of course the INSERT is irrelevant because the entity already exists in database.
If I remove the cascade (cascade = [CascadeType.PERSIST, CascadeType.MERGE]), the bug doesn’t appear but it could lead to data not being saved in some cases.

Sample project: bugInsert.zip (96.1 KB)
step to reproduce:
1- Create a new zip code
2- pick a city
3- save it

Hello,

Thank you for reporting the problem and for the sample project, it helps a lot!
The issue has been created.

For now, as a workaround, the simplest way is to remove cascade persist and control saving manually in places when these entities created by your beans. Editor screens usually take care of it automatically.

Regards,
Dmitry