Saving entity with a onetomany collection fail the validation
Activity
Show:
Alessandro Locatelli July 11, 2023 at 10:37 AM
Hi @Marco Belladelli, no it is a new project so i can’t tell you if it work with previous version of hibernate.
I’ll open a issue to validator teams, thanks
Marco Belladelli July 11, 2023 at 10:16 AMEdited
Hello @Alessandro Locatelli, did this use to work with previous versions of Hibernate? I would suggest opening an issue in Hibernate Validator’s Jira, since the main issue is with that component, and you can then link it to this one.
If it turns out this issue is caused specifically by ORM we will gladly look into it.
Alessandro Locatelli July 11, 2023 at 9:01 AM
Getter and setter in the entities class are omitted
Details
Details
Assignee
Unassigned
UnassignedReporter
Alessandro Locatelli
Alessandro LocatelliLabels
Components
Priority
Created July 11, 2023 at 8:56 AM
Updated July 11, 2023 at 10:40 AM
Hi.
I’ve a problem with entity, collection and validation.
My entities are (Getter and setter in the entities class are omitted):
@Entity public class A { @Id private String id; @NotEmpty @OneToMany(mappedBy = "a", cascade = CascadeType.ALL, orphanRemoval = true) private List<B> bs = new ArrayList<>(); } @Entity public class B { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "a_id", nullable = false) private A a; }
I want to save the entity A.
Before saving it, i put in the bs collection some data.
I also call the validate method on A before the save and it validates without problem.
Next, i call the save method but it failes because the validation @NotEmpty on bs.
I’ve look at DefaultMergeEventListener code and i presume that the problem is at method entityIsTransient:
final Object copy = copyEntity( copyCache, entity, session, persister, id ); // cascade first, so that all unsaved objects get their // copy created before we actually copy //cascadeOnMerge(event, persister, entity, copyCache, Cascades.CASCADE_BEFORE_MERGE); super.cascadeBeforeSave( session, persister, entity, copyCache ); copyValues( persister, entity, copy, session, copyCache, ForeignKeyDirection.FROM_PARENT ); saveTransientEntity( copy, entityName, event.getRequestedId(), session, copyCache ); // cascade first, so that all unsaved objects get their // copy created before we actually copy super.cascadeAfterSave( session, persister, entity, copyCache ); copyValues( persister, entity, copy, session, copyCache, ForeignKeyDirection.TO_PARENT );
The first copyValues call (line 7) does not copy the bs collection from entity to copy object.
When the saveTransientEntity is called (line 9) the validation is triggered but the collection is empty.
It is a bug or a wanted behaviour?
What can i do?
Thanks