# Unnecessary queries with bidirectional relations

**URL:** <https://discourse.hibernate.org/t/unnecessary-queries-with-bidirectional-relations/12092>\
**Category:** Hibernate ORM\
**Created:** [January 9, 2026, 3:16pm UTC](https://discourse.hibernate.org/t/unnecessary-queries-with-bidirectional-relations/12092 "2026-01-09T15:16:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![MajdDalloul](https://avatars.discourse-cdn.com/v4/letter/m/f08c70/32.png) [@MajdDalloul](https://discourse.hibernate.org/u/MajdDalloul)\
**Post date:** [January 9, 2026, 3:16pm UTC](https://discourse.hibernate.org/t/unnecessary-queries-with-bidirectional-relations/12092/1 "2026-01-09T15:16:37Z")

</div>

when this line of code

```auto
user.status = unitOfWork.getReference(UserStatusEntity::class.java, if(type != UserTypeEnum.Driver) UserStatusEnum.Pending.id else UserStatusEnum.Active.id)

```

get executed I am getting unnecessary queries!! (unitOfWork.getReference = entityManager.getReference)  
first query to get the status by id, even that I am using getReference!!

`select use1_0.id, use1_0.title_ar, use1_0.title_en from user_statuses use1_0 where use1_0.id=?`

second query I don’t know why it’s geting the user by status id:

`select u1_0.status_id, u1_0.id, u1_0.created_at, u1_0.email, u1_0.firebase_token, u1_0.gender_id,`  
`u1_0.is_profile_completed, u1_0.password, u1_0.phone_number, u1_0.type_id, u1_0.updated_at`  
`from auth_users u1_0 where u1_0.status_id=?`

if I make it unidirectional relation, it will work as expected no queries at all.  
I want to keep the releation bidirectional and not doing this queries.  
this is my entities:

```auto
@Entity
@Table(
    name = "auth_users",
    indexes = [
        Index(name = "idx_auth_users_phone_number", columnList = "phone_number", unique = true),
        Index(name = "idx_auth_users_email", columnList = "email"),
        Index(name = "idx_auth_users_type", columnList = "type_id"),
    ]
)
class AuthUserEntity(
    @Column(nullable = false, unique = true)
    var phoneNumber: String,

    var email: String? = null,

    var firebaseToken: String? = null,
) : Auditable2Entity() {

    @Id
    @Column(columnDefinition = "uuid")
    var id: UUID? = null

    @Column(nullable = false)
    var password: String? = null

    @Column(nullable = false)
    var isProfileCompleted: Boolean = false

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "status_id", referencedColumnName = "id", nullable = false)
    var status: UserStatusEntity? = null

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "type_id", referencedColumnName = "id")
    var type: UserTypeEntity? = null

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "gender_id", referencedColumnName = "id")
    var gender: GenderEntity? = null

    @OneToOne(mappedBy = "user", fetch = FetchType.LAZY, optional = true)
    var driver: DriverEntity? = null

    @OneToOne(mappedBy = "user", fetch = FetchType.LAZY, optional = true)
    var rider: RiderEntity? = null

    @OneToOne(mappedBy = "user", fetch = FetchType.LAZY, optional = true)
    var partner: PartnerEntity? = null

    @OneToMany(mappedBy = "user", cascade = [CascadeType.ALL], orphanRemoval = true)
    var roles: MutableSet<UserRoleEntity>? = null

    @OneToOne(mappedBy = "user", fetch = FetchType.LAZY, optional = true)
    var wallet: WalletEntity? = null

    @OneToOne(mappedBy = "referralBy", fetch = FetchType.LAZY, optional = true)
    var referralDriver: DriverEntity? = null

    @OneToOne(mappedBy = "referralBy", fetch = FetchType.LAZY, optional = true)
    var referralRider: RiderEntity? = null
}

```

```auto
@Entity
@Table(name = "user_statuses")
class UserStatusEntity(
) : SharedEntity() {
    @Id
    var id: Int? = null
    @Column(nullable = false)
    var titleAr: String? = null
    @Column(nullable = false)
    var titleEn: String? = null

    @OneToMany(mappedBy = "status")
    var users: MutableSet<AuthUserEntity>? = null
}

```

```auto
@Component
class UnitOfWork(
    val entityManager: EntityManager,
    val lookups: LookupRepository, val users: UserRepository,
    val partners: PartnerRepository, val drivers: DriverRepository,
    val riders: RiderRepository, val categories: CategoryRepository,
    val fares: FareRepository, val preferredLocations: PreferredLocationRepository,
    val wallets: WalletRepository, val coupons: CouponRepository,
    val vehicles: VehicleRepository, val orders: OrderRepository,
    val scheduledRides: ScheduledRideRepository, val files: FileRepository,
    val documents: DocumentRepository, val otps: OTPRepository
) {
    fun <T> getReference(entityClass: Class<T>, primaryKey: Any): T = entityManager.getReference(entityClass, primaryKey)
}

```

in my build.gradle.kts I enabled bytecode `enhancement`

`hibernate {`  
` enhancement {`  
` enableLazyInitialization.set(true)`  
` enableDirtyTracking.set(true)`  
` enableAssociationManagement.set(true)`  
` }`  
`}`

---

<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 9, 2026, 4:51pm UTC](https://discourse.hibernate.org/t/unnecessary-queries-with-bidirectional-relations/12092/2 "2026-01-09T16:51:39Z")

</div>

Please try to create a reproducer with our [test case template](https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-7/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java) and if you are able to reproduce the issue, create a bug ticket in our [issue tracker](https://hibernate.atlassian.net) and attach that reproducer.
