Ronaldo Veras
Ronaldo Veras

Reputation: 21

Hibernate @Formula isn't working

In this hibernate pojo the @Formula isn't working for the PAIS_CD parameter, in runtime:

@Entity
@Table(name = "PAISES")
public class Pais {


    @Id
    @Column(name = "PAIS_CD", unique = true, nullable = false, precision = 3, scale = 0)
    private Integer codigo;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "pais",     orphanRemoval = true)
    @OrderBy("dtInicioVigencia desc")
    private List<NomePais> nomesPais = new ArrayList<NomePais>(0);



    @Formula(value = "(select nmps_nm from ( select nmps_nm, nmps_dt_inicio, nmps_dt_fim," +
     "(case when TRUNC(current_date) between np.NMPS_DT_INICIO and     COALESCE(np.NMPS_DT_FIM, TO_DATE('01/01/9999')) THEN 0 " + 
    "when ABS(TRUNC(CURRENT_DATE) - np.NMPS_DT_INICIO) > ABS(TRUNC(CURRENT_DATE) -   COALESCE(np.NMPS_DT_FIM, TO_DATE('02/01/0001'))) " + 
           "THEN ABS(TRUNC(CURRENT_DATE) - COALESCE(np.NMPS_DT_FIM, TO_DATE('02/01/0001'))) " + 
           "ELSE ABS(TRUNC(CURRENT_DATE) - np.NMPS_DT_INICIO) end) " + 
    "from nome_paises np where np.NMPS_PAIS_CD = PAIS_CD " +
    "order by 4, np.NMPS_DT_INICIO desc " +
    ") where ROWNUM = 1)")
    @Transient
    private String nomePaisVigente;
...
}

My suspect is that third-level subselect in @Formula isn't supported for Hibernate. Some solutions for this case?

Using hibernate-core: 3.5.1-Final and hibernate-jpa-2.0-api: 1.0.0-Final

Upvotes: 2

Views: 1101

Answers (2)

Ronaldo Veras
Ronaldo Veras

Reputation: 21

The hibernate query is generated. The problem is that PAIS_CD parameter is not recognized.

Upvotes: 0

Ken Chan
Ken Chan

Reputation: 90427

Field that is annotated with @Transient is ignored by hibernate.Please try to remove @Transient

Upvotes: 2

Related Questions