In the db (mariadb) I have a view defined as a select of data coming from the join of several tables:
CREATE VIEW `obiettivideco` AS
SELECT
`codici`.`id` AS `idc`,
`codici`.`codice` AS `codice`,
`codici`.`sub` AS `sub`,
`codici`.`descrizione` AS `descrizionec`,
`eo`.`id` AS `ide`,
`eo`.`Descrizione` AS `descrizionee`,
`obiettivi`.`id` AS `ido`,
`obiettivi`.`inizio` AS `inizio`,
`obiettivi`.`fine` AS `fine`,
`obiettivi`.`valore` AS `valore`
FROM (
(`obiettivi` JOIN `eo`
ON((`obiettivi`.`eo` = `eo`.`id`))) JOIN `codici`
ON((`obiettivi`.`codice` = `codici`.`id`))
);
Above that layer I generated entities from tables (and from the view as well) in order to have the Model of the MVC pattern. Then in the Controller made with some managed beans I wrote some logic that enables the view (JSF) to show a selection of the above view. When data has to be modified changes are persisted with the help of the tables entities (e.g.
em.getTransaction().begin();
em.persist(obiettivo);
em.getTransaction().commit();
where obiettivo is an object of Class Obiettivi which, in turn, is the entity modelling the obiettivi table. The problem arises if I try to display an update of the obiettivideco selection: jpa cache seems to not be aware that changes in obiettivo will make obiettivideco out of date as well.
I spent a couple of days before I find this answer related to the entitymanager refresh that solved my problem:
q.setHint(QueryHints.REFRESH, true);
obiettivideco = (List<Obiettivideco>) q.getResultList();
Becouse of my low reputation I could not upvote that answer so I'm posting this already answered question in the hope that would save someone else headhaches.