2016-05-15 7 views
1

を維持するために:春ブーツ+ Hibernateは - 私はPOJOクラスを持ってどのように@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が入っているセットではなく、空のセットが返されることです。 どうすれば入手できますか?

+0

キャッシングを有効にしましたか? –

+1

いいえ私はそれを有効にしていません – besmart

+0

私は100%確信していませんが、@Transientプロパティは永続化されていないので一時的に値を保持しているだけで、必要な結果を得るためには、 2回目のコールで引き続きアクセスして変更することができます。あなたのケースでは、 'Interruttore'がデータベースからリフレッシュされ、キャッシュされた値を保持します。キャッシュを使用して何が起こるかを確認してください... –

答えて

0

実際、@TransientはAccessTypeに従って適用する必要があります。

フィールドに@Transientを設定してみてください。

@Transient 
private Set<Integer> activeSensors = new HashSet<Integer>(); 
+0

私はフィールドに私は 'org.hibernate.MappingExceptionを取得している:java.util.Set、テーブル:interruttori、列:[org.hibernate.mapping.Column(active_sensors)] ' – besmart

+0

@Cacheableアノテーションを使ってみてください。これはかなりシンプルです。ドキュメントhttp://docs.spring.io/spring/docs/3.1.0.RC1/spring-framework-reference/html/cache.htmlを参照してください。 – shankarsh15

関連する問題