Hibernate: @Formula query field using another @Formula field

前端 未结 1 1680
無奈伤痛
無奈伤痛 2021-01-18 12:01

I want field idEmpresa to be a @Formula field using idDepartamento (that is another @Formula field) in WHERE statement.



        
1条回答
  •  盖世英雄少女心
    2021-01-18 12:27

    It seems that you are trying to create a valid HQL / JPQL statement, but if you read the JavaDoc of Formula you will see:

    The formula has to be a valid SQL fragment

    So you can't reference any field from your class, only tables and columns from your database.

    But you can change your Formula to use a JOIN instead:

    @Formula("(SELECT ud.IdDepartamento FROM UsuarioDpto ud WHERE ud.PorDefecto = 'S' AND ud.IdUsuario = idUsuario)")
    public String getIdDepartamento() {
        return idDepartamento;
    }
    
    @Formula("(SELECT d.IdEmpresa FROM Departamento d JOIN UsuarioDpto ud ON d.IdDepartamento = uid.idDepartamento WHERE ud.PorDefecto = 'S' AND ud.IdUsuario = idUsuario)")
    public String getIdEmpresa() {
        return idEmpresa;
    }
    

    And I would remove the setters of your formulas, as they will confuse any user of your API. Formula fields are usually read-only.

    0 讨论(0)
提交回复
热议问题