Hibernate won't save entity with ElementCollection of embedded type

28 Views Asked by At

We can't persist records for this entity (DateRangeTemplate). It has a collection of an embedded type.

Hibernate is creating an audit table (DateRangeTemplate_dateRanges_AUD) even though auditing is turned off for these entities.

Version: 6.3.1

All of our other embedded types work fine but they are not referenced as a collection. @ElementCollection also works fine for non-embedded types.

We don't see anything wrong with the code. Any ideas how to fix this?

Do we need to change the embedded type to a table?

@Audited( targetAuditMode = RelationTargetAuditMode.NOT_AUDITED )
@Entity
@Table( name = "date_range_template" )
public class DateRangeTemplate {

    @Id
    @GeneratedValue( strategy = GenerationType.SEQUENCE, generator = "date_range_template_gen" )
    @SequenceGenerator( name = "date_range_template_gen", sequenceName = "date_range_template_seq", allocationSize = 5, initialValue = 1 )
    @Column( name = "id" )
    private Long id;

    @ElementCollection( fetch = FetchType.EAGER )
    @Column( name = "date_ranges" )
    private List<DateRange> dateRanges;
}

@Audited( targetAuditMode = RelationTargetAuditMode.NOT_AUDITED )
@Embeddable
@Access( AccessType.PROPERTY )
public class DateRange {

    @Temporal( TemporalType.DATE )
    @Column( name = "start_date" )
    private LocalDate startDate;

    @Temporal( TemporalType.DATE )
    @Column( name = "end_date" )
    private LocalDate endDate;
}

Hibernate Errors:

A different object with the same identifier value was already associated with the session : 
[
    DateRangeTemplate_dateRanges_AUD#
    {
        SETORDINAL=1, 
        REV=DefaultRevisionEntity(id = 119, revisionDate = Feb 18, 2024, 9:49:45 AM), 
        DateRangeTemplate_id=1, 
        REVTYPE=ADD
    }
]
A different object with the same identifier value was already associated with the session : 
[
    DateRangeTemplate_dateRanges_AUD#
    {
        SETORDINAL=1, 
        REV=DefaultRevisionEntity(id = 120, revisionDate = Feb 18, 2024, 9:49:45 AM), 
        DateRangeTemplate_id=2, 
        REVTYPE=ADD
    }
]

Update: It looks like the problem is that embedded entities have no row id and therefore have to be added to the ElementCollection one at a time with an update(merge) for each add. So if you are adding 1,000 rows, that's 1,000 updates! I guess this is an argument for not using embedded.

0

There are 0 best solutions below