I'm implementing a web app based on spring 3.2.8, jpa2.1 and hibernate 4.3.5. I'd like to know how I can prevent hibernate caching a specific Named Stored Procedure Query
.
I'd tried entityManager.clear(), but I think it clears all cached entity values so it can't be an optimal solution!!
Notice: I used @NamedStoredProcedureQueries
to define stored procedures and call it by
entityManager.createNamedStoredProcedureQuery()
method to get results
Added:
Table person:
person{p# as PK, ...}
Table person_value:
person_value{pv# as PK, p# as foreign key from table person, ...}
View view1:
select *,check_value(dbo.person.p#) as has_value from person
Function check_value(input):
returns 1 if there's a record in person_value with p# equals to input.
Stored procedure SPS_VIEW1:
select * from view1
where p#=@p#
Entity class of sql server view view1:
@Entity
@Table(name = "view1")
@NamedStoredProcedureQueries(
@NamedStoredProcedureQuery(name = "getV1Records", procedureName = "SPS_VIEW1",
resultClasses = View1.class,
@parameters = {@StoredProcedureParameter(name = "p#", type = String.class)))
public class View1{
@Id
@Column(name = "p#")
private String personId;
@Column(name = "hasValue")
private boolean value;
<getters and setters>
}
Service class:
@Service("personService")
public class PersonService() {
...
private EntityManager entityManager;
<getters and setters>
...
public List<View1> getAll(String pno) {
SqlParameterValue p = new SqlParameterValue(new SqlParameter("p#", Types.VARCHAR), pno);
StoredProcedureQuery spq = entityManager.createNamedStoredProcedureQuery("getV1Records");
spq.setParameter(p.getName(), p.getValue());
return list;
}
}
Controller class:
@Controller("PersonController")
public class PersonController {
...
@Autowired
private PersonService personService;
...
@RequestMapping("/person")
public personInfo(HttpServletRequest request) {
String pno = request.getParameter("pno");
ModelAndView mv = new ModelAndView("person");
List<View1> list = personService.getAll(pno);
mv.addObject("view1_list", list);
return mv;
}
...
}
When I call getAll()
with p# e.g. 'p14521' in my controller for first time, there's no record for p# 'p14521' in table person_value, it'll works correctly and every thing is ok, but when I add a record to person_value with p# 'p14521', now there is a record for p# 'p14521', and call getAll()
again nothing will happen to the output list. It seems the out put list of the query is cached.