# @TenantId filtering when loading by Primary Key

**URL:** <https://discourse.hibernate.org/t/tenantid-filtering-when-loading-by-primary-key/9250>\
**Category:** Hibernate ORM\
**Created:** [March 24, 2024, 9:20pm UTC](https://discourse.hibernate.org/t/tenantid-filtering-when-loading-by-primary-key/9250 "2024-03-24T21:20:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevinm416](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/kevinm416/32/2892_2.png) [@kevinm416](https://discourse.hibernate.org/u/kevinm416)\
**Post date:** [March 24, 2024, 9:20pm UTC](https://discourse.hibernate.org/t/tenantid-filtering-when-loading-by-primary-key/9250/1 "2024-03-24T21:20:09Z")

</div>

Hello, I’m using Hibernate 6.4.4.Final and CurrentTenantIdentifierResolver to develop a multi-tenant application. I’m not seeing the expected filtering by `@TenantId` happening when loading entities by their primary key.

For example I get this sql generated for an entity that has a `@TenantId` column:

```auto
["select cb1_0.id,cb1_0.created_by_id,...cb1_0.name,cb1_0.tenant_id 
from carrier cb1_0 where cb1_0.id=?"], Params:[(2)]

```

I would expect that to have another `where tenant_id = ?` clause, like the non-primary key queries do. Non-primary key queries do append the `where tenant_id = ?` clause on as expected, for example:

```auto
["select cb1_0.id,cb1_0.created_by_id,...cb1_0.name,cb1_0.tenant_id 
from carrier cb1_0 join carrier_transportation_type cttb1_0 on cttb1_0.tenant_id = ? and cb1_0.id=cttb1_0.carrier_id 
where cb1_0.tenant_id = ? and (? is null or ?=cttb1_0.transportation_type) and (? is null or lower(cb1_0.name) like ? escape '') offset ? rows fetch first ? rows only"], Params:[(1,1,NULL(VARCHAR),NULL(VARCHAR),NULL(VARBINARY),NULL(VARCHAR),0,20)]

```

I’ve followed the query down into `LoaderSelectorBuilder` where I see that `loadQueryInfluencers` is populated with a `_tenantId` filter. That filter doesn’t seem to be applied the query, and I haven’t made much progress following the code after that.

 ![Screenshot 2024-03-24 at 2.11.28 PM](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/2X/0/076d14680cacd0977e5964d45730afce13ac70df.jpeg)

Is this expected behavior or a bug with the `@TenantId` system?  
Where in the code should the `@TenantId` filtering be applied for this type of query?

---

<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 25, 2024, 1:20pm UTC](https://discourse.hibernate.org/t/tenantid-filtering-when-loading-by-primary-key/9250/2 "2024-03-25T13:20:32Z")

</div>

This is a known limitation of the current implementation. You can follow [[HHH-16830] - Hibernate JIRA](https://hibernate.atlassian.net/browse/HHH-16830) for updates on this topic.

---

<div class="post-metadata">

**Author:** ![kevinm416](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/kevinm416/32/2892_2.png) [@kevinm416](https://discourse.hibernate.org/u/kevinm416)\
**Post date:** [March 25, 2024, 2:03pm UTC](https://discourse.hibernate.org/t/tenantid-filtering-when-loading-by-primary-key/9250/3 "2024-03-25T14:03:39Z")

</div>

Was this known limitation mentioned somewhere in the docs that I missed?

---

<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 25, 2024, 3:46pm UTC](https://discourse.hibernate.org/t/tenantid-filtering-when-loading-by-primary-key/9250/4 "2024-03-25T15:46:43Z")

</div>

The general thought process is that, if the caller to `Session.find()` knows an internal primary key, then it must have gotten it from a secure context i.e. you shouldn’t run `Session.find()` if you’re unsure that the caller is allowed to access the data. You should rather run a query.

We acknowledge the desire to apply the tenant filter though, which is why HHH-16830 is being discussed.

---

<div class="post-metadata">

**Author:** ![kevinm416](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/kevinm416/32/2892_2.png) [@kevinm416](https://discourse.hibernate.org/u/kevinm416)\
**Post date:** [March 25, 2024, 5:28pm UTC](https://discourse.hibernate.org/t/tenantid-filtering-when-loading-by-primary-key/9250/5 "2024-03-25T17:28:08Z")

</div>

Can I submit a PR to change the docs for the @TenantId feature to highlight this limitation for other users? I don’t immediately see where the source for this document is hosted: [Hibernate ORM User Guide](https://docs.jboss.org/hibernate/orm/6.4/userguide/html_single/Hibernate_User_Guide.html#multitenacy-hibernate-TenantId). The docs as they are currently written really make it seem like this should work.

---

<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 25, 2024, 5:47pm UTC](https://discourse.hibernate.org/t/tenantid-filtering-when-loading-by-primary-key/9250/6 "2024-03-25T17:47:22Z")

</div>

Sure you can, [here are the docs](https://github.com/hibernate/hibernate-orm/blob/6.4/documentation/src/main/asciidoc/userguide/chapters/pc/PersistenceContext.adoc). Note though, that we are considering to merge the PR for [[HHH-16830] - Hibernate JIRA](https://hibernate.atlassian.net/browse/HHH-16830) for the final 6.5 release, so if you want to provide a PR, better provide it against the 6.4 branch.
