In my application I have to use one filter condition (columnABC=:inputValue) for all SELECT/INSERT/UPDATE/DELETE queries. Column ‘columnABC’ exist in all tables within application.
I have defined filter def in package-info file
@FilterDef(
name = "columnFilter",
parameters = @ParamDef(name = "columnABC", type = "int"),
defaultCondition = "columnABC = :columnABC")
package com.example.entities;
import org.hibernate.annotations.FilterDef;
import org.hibernate.annotations.ParamDef;
Defined filter on entity
@Filter(name = "columnFilter")
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "table1")
public class Entity1 implements Serializable {
----
---
@Column(name = "columnABC")
private Integer ColumnABC;
}
@Component
public class Repository1 {
@PersistenceContext
private EntityManager entityManager;
public void setFilter(Integer columnABC) {
Session session = entityManager.unwrap(Session.class);
Filter filter = session.enableFilter("columnFilter");
filter.setParameter("columnABC", columnABC);
}
}
Filter is attached to session and it is getting appended for all findAll methods.For findById method it was not working so I have applied @Query and filter has attached to it.When I am running save statements filter is not getting attached to the query. What other option I can use to attach filter to update query.
@Service
public class Service1{
@Autowired
private Entity1Repository entity1Repository;
----
----
entity1Repository.save(Entity1);
}
update
table1
set
column1=?,
column2=?,
column3=?,
---
where
column_id=?