@NotFound used with @OneToMany error

We are upgrading our application to Hibernate 6.1.7 and got the same error as reported here:
HHH-16258

So we tested to use Hibernate Core 6.2.0.CR4 to see if the above error was fixed but with that version our application does not even start anymore due to this error:
Caused by: org.hibernate.AnnotationException: Collection 'com.perfri.testapp.jpa.entity.Carrier.projects' annotated '@NotFound' is not a '@ManyToMany' association

We have lots of bidirectional relations in our application that uses @OneToMany and @ManyToOne together with @NotFound(IGNORE) but according to this exception it seems not to be okay anymore?

Stacktrace from the error is here:

Caused by: org.hibernate.AnnotationException: Collection 'com.perfri.testapp.jpa.entity.Carrier.projects' annotated '@NotFound' is not a '@ManyToMany' association
	at org.hibernate.boot.model.internal.CollectionBinder.notFoundAction(CollectionBinder.java:364) ~[hibernate-core-6.2.0.CR4.jar:6.2.0.CR4]
	at org.hibernate.boot.model.internal.CollectionBinder.bindCollection(CollectionBinder.java:282) ~[hibernate-core-6.2.0.CR4.jar:6.2.0.CR4]
	at org.hibernate.boot.model.internal.PropertyBinder.bindProperty(PropertyBinder.java:851) ~[hibernate-core-6.2.0.CR4.jar:6.2.0.CR4]
	at org.hibernate.boot.model.internal.PropertyBinder.buildProperty(PropertyBinder.java:765) ~[hibernate-core-6.2.0.CR4.jar:6.2.0.CR4]
	at org.hibernate.boot.model.internal.PropertyBinder.processElementAnnotations(PropertyBinder.java:686) ~[hibernate-core-6.2.0.CR4.jar:6.2.0.CR4]
	at org.hibernate.boot.model.internal.EntityBinder.processIdPropertiesIfNotAlready(EntityBinder.java:961) ~[hibernate-core-6.2.0.CR4.jar:6.2.0.CR4]
	at org.hibernate.boot.model.internal.EntityBinder.handleIdentifier(EntityBinder.java:302) ~[hibernate-core-6.2.0.CR4.jar:6.2.0.CR4]
	at org.hibernate.boot.model.internal.EntityBinder.bindEntityClass(EntityBinder.java:228) ~[hibernate-core-6.2.0.CR4.jar:6.2.0.CR4]
	at org.hibernate.boot.model.internal.AnnotationBinder.bindClass(AnnotationBinder.java:417) ~[hibernate-core-6.2.0.CR4.jar:6.2.0.CR4]
	at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:255) ~[hibernate-core-6.2.0.CR4.jar:6.2.0.CR4]
	at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:271) ~[hibernate-core-6.2.0.CR4.jar:6.2.0.CR4]
	at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:314) ~[hibernate-core-6.2.0.CR4.jar:6.2.0.CR4]
	at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1380) ~[hibernate-core-6.2.0.CR4.jar:6.2.0.CR4]
	at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1451) ~[hibernate-core-6.2.0.CR4.jar:6.2.0.CR4]
	at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:66) ~[spring-orm-6.0.6.jar:6.0.6]
	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:376) ~[spring-orm-6.0.6.jar:6.0.6]
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-6.0.6.jar:6.0.6]
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-6.0.6.jar:6.0.6]
	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:352) ~[spring-orm-6.0.6.jar:6.0.6]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1808) ~[spring-beans-6.0.6.jar:6.0.6]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1758) ~[spring-beans-6.0.6.jar:6.0.6]
	... 124 common frames omitted

This annotation never did anything for @OneToMany or @ManyToMany, and we wanted to tell users to get rid of it, since it might be confusing to readers of the model, giving a wrong impression about behavior.

1 Like

Sorry, but You wrong :wink:

We use this heavily to suppress errors caused by missing cache evictions, because correcting all such errors is very difficult to achieve in a large application. So, this is blocker for us to upgrade to Hibernate 6 :frowning:

@OneToMany(cascade = {CascadeType.REMOVE}, fetch = FetchType.LAZY)
    @JoinColumn(name = "project_id")
    @JsonIgnore
    @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    @NotFound(action = NotFoundAction.IGNORE)
    private Set<Branch> branches;

As far as I can see, support for @OneToMany + @NotFound was removed via HHH-15509 correctly support @NotFound @ManyToMany · hibernate/hibernate-orm@7d34f86 · GitHub as part of HHH-15545

Please comment on that issue and explain why you need this. I personally don’t understand why the @NotFound functionality was only retained for @ManyToMany since a @OneToMany can also have a join table, but I’m sure you’ll get an answer when asking there.