admin管理员组

文章数量:1122832

I am trying to define @Filter on Entity,and it's not working on my test code

This is my entity:

@Entity
@FilterDef(
    name = "tenantFilter",
    parameters = [ParamDef(name = "tenantId", type = "string")],
)
@Filters(
    value = [
        Filter(name = "tenantFilter", condition = "tenant_id = :tenantId")
    ]
)
class DemoEntity {
   lateinit var tenantId: String
}

This is my test code:

        var demoEntity = DemoEntity()
        demoEntity.id = "1"
        demoEntity.tenantId = "tenant1"
        demoRepository.save(demoEntity)
        demoEntity = DemoEntity()
        demoEntity.id = "2"
        demoEntity.tenantId = "tenant2"
        demoRepository.save(demoEntity)


        val session = entityManager.unwrap(Session::class.java)
        val enableFilter = session.enableFilter("tenantFilter")
        enableFilter.setParameter("tenantId", "tenant1")
        val query = entityManager.createQuery("from DemoEntity ").resultList
        session.disableFilter("tenantFilter")

        println(query)

Ouput:

[DemoEntity(id='1', tenantId='tenant1', createdAt=2024-11-22T11:35:24.902234, updatedAt=2024-11-22T11:35:24.902234), DemoEntity(id='2', tenantId='tenant2', createdAt=2024-11-22T11:35:24.959239, updatedAt=2024-11-22T11:35:24.959239)]

本文标签: javaHibernate Filter Not Working as ExpectedStack Overflow