有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaSpringBoot+Hibernate如何维护@Transient字段

我有一门POJO课程:

@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的集合。 我怎样才能得到它


共 (1) 个答案

  1. # 1 楼答案

    实际上,应根据AccessType应用@Transient

    请尝试将@Transient设置为字段

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