0

I have the following table:

CREATE TABLE "pre_vendas" (
    "id_prev_venda" BIGINT NOT NULL,
    "data_prev_venda" DATE NOT NULL,
    "data_real_venda" DATE NOT NULL,    
    "valor_real_venda" NUMERIC(10,2) NULL DEFAULT NULL, 
    PRIMARY KEY ("id_prev_venda")
);

PreVenda.class

@Entity
@Table(name = "pre_vendas")
@XmlRootElement
public class PreVenda implements Serializable {

    private static final long serialVersionUID = 1345540614644262508L;

    @Id
    @GeneratedValue(generator = "pre_venda_seq_gen")
    @GenericGenerator(name = "pre_venda_seq_gen", strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator", parameters = {
            @Parameter(name = "sequence_name", value = "seq_pre_venda"), @Parameter(name = "optimizer", value = "hilo"),
            @Parameter(name = "initial_value", value = "1"), @Parameter(name = "increment_size", value = "1") })
    @Column(name = "id_prev_venda", nullable = false)
    private Long idPrevVenda;

    @Column(name = "data_prev_venda", nullable = false)
    @Temporal(TemporalType.DATE)
    private Date dataPrevVenda;

    @Column(name = "data_real_venda", nullable = false)
    @Temporal(TemporalType.DATE)
    private Date dataRealVenda;

    @Column(name = "valor_real_venda", nullable = false, precision = 10, scale = 2)
    private BigDecimal valorRealVenda;

}

I’m having difficulty converting the dates in numeric format (numbers only), I’m trying this way:

CriteriaBuilder builder = sf.getCurrentSession().getCriteriaBuilder();
CriteriaQuery<Tuple> criteria = builder.createTupleQuery();
Root<PreVenda> root = criteria.from(PreVenda.class);

Expression<Long> dataPrevVenda = root.get(PreVenda_.dataPrevVenda).as(Long.class);

Expression<Long> dataRealVenda = root.get(PreVenda_.dataRealVenda).as(Long.class);

criteria.select(builder.tuple(builder.diff(dataRealVenda,dataPrevVenda)));

this is returning a cast exception, Anyone have any idea how to solve this?

Mauro
  • 13
  • 1
  • 4
  • 1
    Please, add the PreVenda class with the hibernate mapping in order to check if there are any differences with the SP definition – Sebastian D'Agostino May 21 '18 at 14:57
  • Hello, Thanks for the reply, I just edited the file with the doubt. – Mauro May 23 '18 at 01:37
  • Could you add the stack trace? I guess the problem is with the BigDecimal attribute. I guess there is a problem with the scale. Check this out: https://stackoverflow.com/questions/6544232/how-do-i-map-a-bigdecimal-in-hibernate-so-i-get-back-the-same-scale-i-put-in – Sebastian D'Agostino May 23 '18 at 13:56

0 Answers0