Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArrayIndexOutOfBounds when saving OneToMany relation and using annotation processor #1964

Open
bryhemm opened this issue Dec 5, 2024 · 2 comments

Comments

@bryhemm
Copy link

bryhemm commented Dec 5, 2024

I've run into an exception when saving an Entity View that contains a OneToMany mapping. The exception only occurs when I'm using the blaze-persistence-entity-view-processor-jakarta annotation processor. If I don't use the annotation processor, everything behaves as expected.

My Entities are:

@Entity
@Data
@NoArgsConstructor
public class Workflow {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column
    private Long id;

    @OneToMany(mappedBy = "workflow")
    private List<WorkflowStep> steps;
}
@Entity
@Data
@NoArgsConstructor
public class WorkflowStep {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column
    private Long id;
    
    @ManyToOne
    @JoinColumn(name="workflow_id")
    private Workflow workflow;
}

And my views are:

@EntityView(Workflow.class)
public interface WorkflowIdView {
        @IdMapping
        Long getId();
}
@EntityView(Workflow.class)
@CreatableEntityView
@UpdatableEntityView
public interface WorkflowView extends WorkflowIdView {
    @UpdatableMapping
    @MappingInverse(removeStrategy = InverseRemoveStrategy.REMOVE)
    List<WorkflowStepView> getSteps()
}
@EntityView(WorkflowStep.class)
@CreatableEntityView
@UpdatableEntityView
public interface WorkflowStepView {
    @IdMapping
    Long getId();

    @UpdatableMapping(cascade = CascadeType.PERSIST)
    WorkflowIdView getWorkflow();
    void setWorkflow(WorkflowIdView workflow);
}

To test this, I'm saving the WorkflowView and as WorkflowStepView at the same time:

        var step1 = evm.create(WorkflowStepView.class);
        var wf = evm.create(WorkflowView.class);
        wf.getSteps().add(step1);

        evm.save(em, wf);

If I don't add a step to the WorkflowView or if I disable the annotation processor, it saves correctly.

The exception I'm seeing is

java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 4
        at com.blazebit.persistence.view.impl.accessor.DirtyStateViewAttributeAccessor.setValue(DirtyStateViewAttributeAccessor.java:81) ~[blaze-persistence-entity-view-impl-jakarta-1.6.14.jar:1.6.14]
        at com.blazebit.persistence.view.impl.mapper.ReferenceViewAttributeMapper.map(ReferenceViewAttributeMapper.java:42) ~[blaze-persistence-entity-view-impl-jakarta-1.6.14.jar:1.6.14]
        at com.blazebit.persistence.view.impl.mapper.CompositeMapper.map(CompositeMapper.java:24) ~[blaze-persistence-entity-view-impl-jakarta-1.6.14.jar:1.6.14]
        at com.blazebit.persistence.view.impl.mapper.TargetViewClassBasedMapper.map(TargetViewClassBasedMapper.java:28) ~[blaze-persistence-entity-view-impl-jakarta-1.6.14.jar:1.6.14]
        at com.blazebit.persistence.view.impl.entity.InverseViewToEntityMapper$1.run(InverseViewToEntityMapper.java:91) ~[blaze-persistence-entity-view-impl-jakarta-1.6.14.jar:1.6.14]
        at com.blazebit.persistence.view.impl.update.flush.CompositeAttributeFlusher.flushEntity(CompositeAttributeFlusher.java:645) ~[blaze-persistence-entity-view-impl-jakarta-1.6.14.jar:1.6.14]
        at com.blazebit.persistence.view.impl.entity.InverseViewToEntityMapper.flushEntity(InverseViewToEntityMapper.java:109) ~[blaze-persistence-entity-view-impl-jakarta-1.6.14.jar:1.6.14]
        at com.blazebit.persistence.view.impl.entity.TargetViewClassBasedInverseViewToEntityMapper.flushEntity(TargetViewClassBasedInverseViewToEntityMapper.java:38) ~[blaze-persistence-entity-view-impl-jakarta-1.6.14.jar:1.6.14]
        at com.blazebit.persistence.view.impl.update.flush.InverseFlusher.flushQuerySetEntityOnElement(InverseFlusher.java:464) ~[blaze-persistence-entity-view-impl-jakarta-1.6.14.jar:1.6.14]
        at com.blazebit.persistence.view.impl.update.flush.InverseFlusher.flushQuerySetEntityOnViewElement(InverseFlusher.java:454) ~[blaze-persistence-entity-view-impl-jakarta-1.6.14.jar:1.6.14]
        at com.blazebit.persistence.view.impl.update.flush.InverseFlusher.flushQuerySetEntityOnElement(InverseFlusher.java:447) ~[blaze-persistence-entity-view-impl-jakarta-1.6.14.jar:1.6.14]
        at com.blazebit.persistence.view.impl.update.flush.CollectionAttributeFlusher$ElementFlusherQueryExecutor.onAddedAndUpdatedInverseElement(CollectionAttributeFlusher.java:1763) ~[blaze-persistence-entity-view-impl-jakarta-1.6.14.jar:1.6.14]
        at com.blazebit.persistence.view.impl.update.flush.CollectionAttributeFlusher.visitInverseElementFlushersForActions(CollectionAttributeFlusher.java:1813) ~[blaze-persistence-entity-view-impl-jakarta-1.6.14.jar:1.6.14]
        at com.blazebit.persistence.view.impl.update.flush.CollectionAttributeFlusher.flushEntity(CollectionAttributeFlusher.java:725) ~[blaze-persistence-entity-view-impl-jakarta-1.6.14.jar:1.6.14]
        at com.blazebit.persistence.view.impl.update.flush.CollectionAttributeFlusher.flushEntity(CollectionAttributeFlusher.java:53) ~[blaze-persistence-entity-view-impl-jakarta-1.6.14.jar:1.6.14]
        at com.blazebit.persistence.view.impl.update.flush.CompositeAttributeFlusher.deferredFlushEntity(CompositeAttributeFlusher.java:857) ~[blaze-persistence-entity-view-impl-jakarta-1.6.14.jar:1.6.14]       
        at com.blazebit.persistence.view.impl.update.flush.CompositeAttributeFlusher.flushEntity(CompositeAttributeFlusher.java:762) ~[blaze-persistence-entity-view-impl-jakarta-1.6.14.jar:1.6.14]
        at com.blazebit.persistence.view.impl.update.EntityViewUpdaterImpl.executePersist(EntityViewUpdaterImpl.java:721) ~[blaze-persistence-entity-view-impl-jakarta-1.6.14.jar:1.6.14]
        at com.blazebit.persistence.view.impl.update.EntityViewUpdaterImpl.executePersist(EntityViewUpdaterImpl.java:716) ~[blaze-persistence-entity-view-impl-jakarta-1.6.14.jar:1.6.14]
        at com.blazebit.persistence.view.impl.EntityViewManagerImpl.update(EntityViewManagerImpl.java:1180) ~[blaze-persistence-entity-view-impl-jakarta-1.6.14.jar:1.6.14]
        at com.blazebit.persistence.view.impl.EntityViewManagerImpl.update(EntityViewManagerImpl.java:1142) ~[blaze-persistence-entity-view-impl-jakarta-1.6.14.jar:1.6.14]
        at com.blazebit.persistence.view.impl.EntityViewManagerImpl.save(EntityViewManagerImpl.java:1059) ~[blaze-persistence-entity-view-impl-jakarta-1.6.14.jar:1.6.14]
        at com.example.demo.WorkflowController.test(WorkflowController.java:32) ~[main/:na]
@beikov
Copy link
Member

beikov commented Dec 6, 2024

Can you please share this example/demo application? That would make it easier to look into.

@bryhemm
Copy link
Author

bryhemm commented Dec 6, 2024

Here is an example project
https://github.com/bryhemm/spring-blaze

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants