# @BatchSize doesn't fix "N+1" with bidirectional One-to-One when querying using JPQL

**URL:** https://discourse.hibernate.org/t/batchsize-doesnt-fix-n-1-with-bidirectional-one-to-one-when-querying-using-jpql/11174
**Category:** Hibernate ORM
**Created:** [March 8, 2025, 11:32am UTC](https://discourse.hibernate.org/t/batchsize-doesnt-fix-n-1-with-bidirectional-one-to-one-when-querying-using-jpql/11174 "2025-03-08T11:32:29Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![gorbunovivan](https://avatars.discourse-cdn.com/v4/letter/g/e19b73/32.png) [@gorbunovivan](https://discourse.hibernate.org/u/gorbunovivan)
#### Post date: [March 8, 2025, 11:32am UTC](https://discourse.hibernate.org/t/batchsize-doesnt-fix-n-1-with-bidirectional-one-to-one-when-querying-using-jpql/11174/1 "2025-03-08T11:32:29Z")

</div>

I’m facing N+1 problem and want to fix it using only Static defition (fetch = FetchType.?, @Fetch(FetchMode.?) and @BatchSize):

Entities:

```auto
	@Entity
	@Table(name = "persons")
	@BatchSize(size = 20)
	@NoArgsConstructor @AllArgsConstructor
	@Getter @Setter
	@ToString
	public class Person {

		@Id
		@GeneratedValue(strategy = GenerationType.IDENTITY)
		private Integer id;

		@OneToOne(mappedBy = "person")
		@Fetch(FetchMode.JOIN)
		private PersonDetails personDetails;
	}

	@Entity
	@Table(name = "person_details")
	@BatchSize(size = 20)
	@NoArgsConstructor @AllArgsConstructor
	@Getter @Setter
	@ToString
	public class PersonDetails {

		@Id
		@GeneratedValue(strategy = GenerationType.IDENTITY)
		private Integer id;
		
		@OneToOne
		@JoinColumn(name = "person_id")
		private Person person;
	}

```

Fetching:

```auto
try (var em = emf.createEntityManager()) {
		em.getTransaction().begin();
		
		var persons = em.createQuery("FROM Person", Person.class).getResultList();
		System.out.println(persons); // 4 persons found

		em.getTransaction().commit();
	}

```

Console output:

> Hibernate: select p1\_0.id from persons p1\_0  
> (and the next query is repeated 4 times (due to “N+1” behavior)):  
> Hibernate: select pd1\_0.id,p1\_0.id from person\_details pd1\_0 left join persons p1\_0 on p1\_0.id=pd1\_0.person\_id where pd1\_0.person\_id=?

I know that “N+1” can be fixed with “FROM Person p JOIN FETCH p.personDetails” or with Entity Graphs.  
But my goal is to understand - can this be fixed with Static definition (fetch = FetchType.?, @Fetch(FetchMode.?) and @BatchSize)

P.S. Why @BatchSize (at the class level) doesn’t help here?

P.S. Hibernate version is “6.6.5.Final”

---

<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 11, 2025, 11:30am UTC](https://discourse.hibernate.org/t/batchsize-doesnt-fix-n-1-with-bidirectional-one-to-one-when-querying-using-jpql/11174/2 "2025-03-11T11:30:22Z")

</div>

Currently, `OneToOne(mappedBy = "...")` associations are implemented as unique key lookups, which do not support batching yet. You can follow [HHH-19235](https://hibernate.atlassian.net/browse/HHH-19235) for updates on this matter.
