# @NotFound used with @OneToMany error

**URL:** https://discourse.hibernate.org/t/notfound-used-with-onetomany-error/7458
**Category:** Hibernate ORM
**Created:** [March 31, 2023, 8:44am UTC](https://discourse.hibernate.org/t/notfound-used-with-onetomany-error/7458 "2023-03-31T08:44:35Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![perfri](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/perfri/32/2351_2.png) [@perfri](https://discourse.hibernate.org/u/perfri)
#### Post date: [March 31, 2023, 8:44am UTC](https://discourse.hibernate.org/t/notfound-used-with-onetomany-error/7458/1 "2023-03-31T08:44:35Z")

</div>

We are upgrading our application to Hibernate 6.1.7 and got the same error as reported here:  
[HHH-16258](https://hibernate.atlassian.net/browse/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:

```auto
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

```

---

<div class="post-metadata">

### Author: ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)
#### Post date: [March 31, 2023, 8:59am UTC](https://discourse.hibernate.org/t/notfound-used-with-onetomany-error/7458/2 "2023-03-31T08:59:11Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![tprus](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/tprus/32/3787_2.png) [@tprus](https://discourse.hibernate.org/u/tprus)
#### Post date: [January 24, 2025, 8:36am UTC](https://discourse.hibernate.org/t/notfound-used-with-onetomany-error/7458/3 "2025-01-24T08:36:04Z")

</div>

Sorry, but You wrong 😉

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 ☹

```auto
@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;

```

---

<div class="post-metadata">

### Author: ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)
#### Post date: [January 24, 2025, 10:43am UTC](https://discourse.hibernate.org/t/notfound-used-with-onetomany-error/7458/4 "2025-01-24T10:43:07Z")

</div>

As far as I can see, support for `@OneToMany` + `@NotFound` was removed via [HHH-15509 correctly support @NotFound @ManyToMany · hibernate/hibernate-orm@7d34f86 · GitHub](https://github.com/hibernate/hibernate-orm/commit/7d34f86a9554820a490c0fd06b001600cf600637) as part of [HHH-15545](https://hibernate.atlassian.net/browse/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.
