CriteriaBuilder投射日期为长

时间:2018-05-21 14:42:43

标签: java hibernate hibernate-criteria jpa-criteria

我有下表:

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;

}

我在以数字格式转换日期时遇到了困难(仅限数字),我正在尝试这种方式:

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)));

这是返回一个强制转换异常,任何人都知道如何解决这个问题?

0 个答案:

没有答案