有 Java 编程相关的问题?

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

java jpa从oracle检索日期

您好,我需要从具有日期值的oracle数据库中检索数据,我有以下实体:

@Entity
@Table(name="GRUPO_HORARIO_FICHA")
public class grupo_horario {

    @Id
    @Column(name = "COD_GRUPO_HORARIO")
    private String codigo;

    @Temporal(TemporalType.TIME) //tried DATE,TIME and TIMESTAMP already
    @Column(name = "HORA_INI")
    private Date hora_ini; //java.util.Date

    @Temporal(TemporalType.TIME) //tried DATE,TIME and TIMESTAMP already
    @Column(name = "HORA_FIN") 
    private Date hora_fin; //java.util.Date

因此,当我在DAO中调用以下方法时,日期值为null:

grupo_horario grupo_horarios= new grupo_horario();
        try {
        grupo_horarios = (grupo_horario) em.createQuery("SELECT g FROM grupo_horario g WHERE g.codigo = :codigo").setParameter("codigo", codigo).getSingleResult();
        } catch (NoResultException e) {return null; }


        System.out.println("el resultado date=" + grupo_horarios.getCodigo()); // i get the id with no problem at all.
        System.out.println("el resultado date=" + grupo_horarios.getHora_ini()); // i get null here, i need to retrieve the time.

数据库中的日期列应该是DATE,而不是TIMESTAMP,我的日期是java。util。日期如果需要这些信息,我会使用ojdbc6和oracle database 10g


共 (1) 个答案

  1. # 1 楼答案

    看起来我的表格栏是HORA_INICIO,这似乎就是问题所在,只是改变了它和它的工作方式