# Hibernate @JoinColumnsOrFormulas throws org.hibernate.AssertionFailure: value involves formulas Exception

**URL:** <https://discourse.hibernate.org/t/hibernate-joincolumnsorformulas-throws-org-hibernate-assertionfailure-value-involves-formulas-exception/8761>\
**Category:** Hibernate ORM\
**Created:** [December 27, 2023, 1:53pm UTC](https://discourse.hibernate.org/t/hibernate-joincolumnsorformulas-throws-org-hibernate-assertionfailure-value-involves-formulas-exception/8761 "2023-12-27T13:53:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bashayam30](https://avatars.discourse-cdn.com/v4/letter/b/9d8465/32.png) [@Bashayam30](https://discourse.hibernate.org/u/Bashayam30)\
**Post date:** [December 27, 2023, 1:53pm UTC](https://discourse.hibernate.org/t/hibernate-joincolumnsorformulas-throws-org-hibernate-assertionfailure-value-involves-formulas-exception/8761/1 "2023-12-27T13:53:04Z")

</div>

I am trying to use Multi Join Columns for a @ManyToOne relationship. My use case is exactly the same as mentioned in this StackOverflow answer - [java - Hibernate composite key and overlapping field - how to avoid column duplication - Stack Overflow](https://stackoverflow.com/questions/54783965/hibernate-composite-key-and-overlapping-field-how-to-avoid-column-duplication).

```auto
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumnsOrFormulas(
        value = {
                @JoinColumnOrFormula(column = @JoinColumn(referencedColumnName = "id", name = "author_id")),
                @JoinColumnOrFormula(formula = @JoinFormula(referencedColumnName = "tenant_id", value = "tenant_id"))
        })
private Entity entity;

```

I followed the same approach of using @JoinColumnsOrFormulas annotation, and it throws this exception during application startup, causing the Spring Boot application not to run -

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘entityManagerFactory’ defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: value involves formulas

Caused by: org.hibernate.AssertionFailure: value involves formulas  
at org.hibernate.mapping.SimpleValue.getColumns(SimpleValue.java:282)  
at org.hibernate.boot.model.internal.TableBinder.mappedByColumns(TableBinder.java:773)  
at org.hibernate.boot.model.internal.TableBinder.bindUnownedAssociation(TableBinder.java:755)  
at org.hibernate.boot.model.internal.TableBinder.bindForeignKey(TableBinder.java:546)  
at org.hibernate.boot.model.internal.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:2587)  
at org.hibernate.boot.model.internal.CollectionBinder.bindOneToManySecondPass(CollectionBinder.java:1642)  
at org.hibernate.boot.model.internal.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:1545)  
at org.hibernate.boot.model.internal.CollectionBinder$1.secondPass(CollectionBinder.java:1534)  
at org.hibernate.boot.model.internal.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:45)  
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1857)  
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1814)  
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:328)  
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1380)  
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1451)  
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:75)

Observed this error while using Hibernate v6.2.6.Final. Also tried downgrading to v6.0.0.CR1 and still seeing the same error.

Any help here would be really appreciated. Thanks in advance.

---

<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 4, 2024, 11:47am UTC](https://discourse.hibernate.org/t/hibernate-joincolumnsorformulas-throws-org-hibernate-assertionfailure-value-involves-formulas-exception/8761/2 "2024-01-04T11:47:48Z")

</div>

You don’t need `@JoinColumnOrFormula`. Just use this mapping instead:

```java
    @IdClass(TenantAwareKey.class)
    @Entity
    @Table(name = "BOOK")
    @Data
    public class Book {

        @Id
        @GeneratedValue
        @Column(name = "ID")
        private UUID id;
        @Id
        @Column(name = "TENANT_ID")
        private Integer tenantId;

        private String title;

        @ManyToOne
        @JoinColumns(
            value = {
                @JoinColumn(referencedColumnName = "id", name = "author_id", insertable = false, updatable = false),
                @JoinColumn(referencedColumnName = "tenant_id", name = "tenant_id", insertable = false, updatable = false)
            })
        private Author author;
        @Column(name = "author_id")
        private UUID authorId;
    }

```

And sync the `authorId` field whenever you set a `author`. The other option you have is to create a separate column `autor_tenant_id` and map it like this:

```java
    @IdClass(TenantAwareKey.class)
    @Entity
    @Table(name = "BOOK")
    @Data
    public class Book {

        @Id
        @GeneratedValue
        @Column(name = "ID")
        private UUID id;
        @Id
        @Column(name = "TENANT_ID")
        private Integer tenantId;

        private String title;

        @ManyToOne
        @JoinColumns(
            value = {
                @JoinColumn(referencedColumnName = "id", name = "author_id"),
                @JoinColumn(referencedColumnName = "tenant_id", name = "author_tenant_id")
            })
        private Author author;
    }

```

---

<div class="post-metadata">

**Author:** ![Bashayam30](https://avatars.discourse-cdn.com/v4/letter/b/9d8465/32.png) [@Bashayam30](https://discourse.hibernate.org/u/Bashayam30)\
**Post date:** [January 4, 2024, 12:16pm UTC](https://discourse.hibernate.org/t/hibernate-joincolumnsorformulas-throws-org-hibernate-assertionfailure-value-involves-formulas-exception/8761/3 "2024-01-04T12:16:03Z")

</div>

Thanks @beikov for the response. The first approach is what we have proceeded with, and it works pretty well.

If @TenantId supports adding the tenant filter for lazy fetching and joins, then there would essentially be no need to do these workarounds. Are there any plans to include this feature with @TenantId annotation?

---

<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 4, 2024, 12:24pm UTC](https://discourse.hibernate.org/t/hibernate-joincolumnsorformulas-throws-org-hibernate-assertionfailure-value-involves-formulas-exception/8761/4 "2024-01-04T12:24:27Z")

</div>

The lazy fetching part is related to this existing feature request which you can track: [[HHH-16830] - Hibernate JIRA](https://hibernate.atlassian.net/browse/HHH-16830)  
Tenant filters for HQL/Criteria joins should be applied though. If that doesn’t happen, that would be a bug IMO.
