Problem with delete record by standard delete action

Hi

I have entity DocTypes with column NUMBERMASK_ID definition like below

@Lookup(type = LookupType.DROPDOWN, actions = {"lookup", "clear"})
@NotNull
@OnDelete(DeletePolicy.DENY)
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "NUMBERMASK_ID")
protected NumberSchema numbermask;

What I try delete record from table DocTypes I get a message

obraz

I haven’t any relation in entity NumberSchema.
Foreing key on mssql is correctly and I can delete this record from managment studio.
What it’s wrong ?

Hi,

According to the annotation @OnDelete(DeletePolicy.DENY) this message is shown when you try to delete an instance which has a non-null value in the numbermask field. Moreover, you have made the field mandatory, so it cannot be null. Please check it.

Hi

I find solution in documentation. I use @OnDeleteInverse annotations and @OneToMany(mappedBy = "DocTypes")
annotations in numbermask entity, but I have to use soft deleted mechanism. Is there any way to use mechanism base
on foreign key ?

Referential integrity on the database level (foreign keys) cannot prevent from soft deletion by definition: soft deletion does not delete records from the database. This is why we need these annotations for soft-deleted entities.

But can i switch off soft delete mechanism and use foreign key only ?

Sure. Inherit your entities from BaseUuidEntity instead of StandardEntity and do not implement SoftDelete:

image

Ok, but when I switch of soft delete mechanism I give a message like below, when
I delete record. I expect then in this case I give message
e.g "Can't delete record because there is relation to other position

com.haulmont.cuba.core.global.RemoteException:
---
org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.2.cuba23): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: The DELETE statement conflicted with the REFERENCE constraint "FK_RENTAL_ARTICLES_GROUPID". The conflict occurred in database "rental", table "dbo.RENTAL_ARTICLES", column 'GROUPID_ID'.
Error Code: 547
Call: DELETE FROM RENTAL_GROUP WHERE ((ID = ?) AND (VERSION = ?))
	bind => [201, 1]
Query: DeleteObjectQuery(com.company.rental.entity.Group-201 [managed,removed])
---
javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.2.cuba23): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: The DELETE statement conflicted with the REFERENCE constraint "FK_RENTAL_ARTICLES_GROUPID". The conflict occurred in database "rental", table "dbo.RENTAL_ARTICLES", column 'GROUPID_ID'.
Error Code: 547
Call: DELETE FROM RENTAL_GROUP WHERE ((ID = ?) AND (VERSION = ?))
	bind => [201, 1]
Query: DeleteObjectQuery(com.company.rental.entity.Group-201 [managed,removed])
---
org.eclipse.persistence.exceptions.DatabaseException: 
Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: The DELETE statement conflicted with the REFERENCE constraint "FK_RENTAL_ARTICLES_GROUPID". The conflict occurred in database "rental", table "dbo.RENTAL_ARTICLES", column 'GROUPID_ID'.
Error Code: 547
Call: DELETE FROM RENTAL_GROUP WHERE ((ID = ?) AND (VERSION = ?))
	bind => [201, 1]
Query: DeleteObjectQuery(com.company.rental.entity.Group-201 [detached,removed])