我有一個POJO類:春天引導+ Hibernate的 - 如何保持@Transient領域
@Entity
@Table(name = "interruttori", catalog = "SMARTPARK")
public class Interruttore implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private int idInterruttore;
private int numeroInterruttore;
private String nomeInterruttore;
private String descrizione;
private List<Luce> luci;
private String pinName;
private boolean remoto;
private boolean stato;
private Date dateTime;
private Set<Integer> activeSensors = new HashSet<Integer>();
//getters and setters and specifically
@Transient
public Set<Integer> getActiveSensors() {
return activeSensors;
}
public void setActiveSensors(Set<Integer> activeSensors) {
this.activeSensors = activeSensors;
}
這是因爲@Transient我不想堅持的設置,我需要它只是作爲一個「反」在控制器中。
我們需要的控制器部分是:
@RequestMapping(value="/sensoristica")
public ResponseEntity<Sensoristica> findIlluminazione(@RequestParam(value="idLuce") int idLuce,
@RequestParam(value="lit") boolean lit,
@RequestParam(value="suServer") boolean suServer) {
Sensoristica sensoristica = new Sensoristica();
Luce luce = luceService.findById(idLuce);
String nomeLuce = luce.getNomeLuce();
int numeroLuce = luce.getNumeroLuce();
sensoristica.setLuce(luce);
sensoristica.setLit(lit);
sensoristicaService.saveSensoristica(sensoristica);
logger.debug("Aggiornato sensore " + numeroLuce + " ("+nomeLuce+") acceso: "+lit);
//aggiorniamo lo stato del sensore
luce.setLit(lit);
luceService.updateLuce(luce);
//qui gestisco l'interruttore
if(suServer){
int idInterruttore = luce.getInterruttore().getIdInterruttore();
Interruttore interruttore = interruttoreService.findById(idInterruttore);
Set<Integer> activeSensors = interruttore.getActiveSensors();
logger.debug("Active sensor è " +activeSensors);
if(lit){
activeSensors.add(idLuce);
logger.debug("Aggiungo id "+idLuce);
logger.debug("Active è lungo: "+activeSensors.size());
} else {
if (activeSensors.contains(idLuce)){
activeSensors.remove(idLuce);
logger.debug("Rimuovo id "+idLuce);
logger.debug("Active è lungo: "+activeSensors.size());
}
}
interruttore.setActiveSensors(activeSensors);
interruttoreService.updateInterruttore(interruttore);
boolean canShutDown = activeSensors.isEmpty();
logger.debug("canShutDown is "+canShutDown);
基本上,我想添加或刪除idLuce
的設置,然後檢查是否activeSensors.isEmpty()
。 問題是每次調用控制器都會給我一個空集,而不是以前的idLuce
s。 我如何獲得它?
您是否啓用了緩存? –
不,我沒有啓用它 – besmart
我不是100%肯定的,但如果@Transient屬性只是暫時保持一個值,因爲它不是持久的,它必須存儲在某個地方,以便獲得您想要的結果,以便第二次調用繼續訪問並更改它。在你的情況下,以便Interruttore從數據庫中刷新並保存緩存的值。嘗試使用緩存,看看會發生什麼... –