数据类型datetime和time在大于或等于运算符时不兼容

时间:2014-04-02 16:27:57

标签: java sql-server hibernate hql

我在数据库表的一列中有一个变量类型的时间。 如何将java中的这个值与此字段进行比较?我的意思是我可以使用date,gregoriancalendar吗? 我已经尝试过了,我仍然有这个消息,请有人给我一个建议

Date d2 = new Date();                      // timestamp now
Calendar cal = Calendar.getInstance();       // get calendar instance
cal.setTime(d2);                           // set cal to date
cal.set(Calendar.HOUR_OF_DAY, 10);            // set hour to midnight
cal.set(Calendar.MINUTE, 30);                 // set minute in hour
cal.set(Calendar.SECOND, 0);                 // set second in minute
cal.set(Calendar.MILLISECOND, 0);            // set millis in second
Date d3 = cal.getTime();   

@SuppressWarnings("unchecked")
List<Asistencia> list = (List<Asistencia>) sessionFactory
.getCurrentSession()
.createQuery(
        "select new Asistencia( asis.idAsistencia,"
        + "asis.horaInicio, asis.horaFin) "
        + "from Asistencia asis "
        + "where :hour >= asis.horaInicio and :hour <= asis.horaFin")

.setParameter("hour", d3).list();

我也在

之间使用
where :hour between asis.horaInicio and asis.horaFin

和消息相同:

ERROR: org.hibernate.engine.jdbc.spi.SqlExceptionHelper - The data types datetime and time are incompatible in the greater than or equal to operator.

数据类型datetime和time在大于或等于运算符时不兼容。

这里是Asistencia课程:

public class Asistencia implements java.io.Serializable {

private static final long serialVersionUID = 1L;
private long idAsistencia;
    private Date horaInicio;
    private Date horaFin;
    private int idAula;
    private int idCurso;
    private int idPeriodo;
    private Date fecha;

    public Asistencia (){
    }
    public Asistencia (long idAsistencia, Date horaInicio, Date horaFin){
          this.idAsistencia
          this.horaInicio = horaInicio;
          this.horaFin = horaFin;
    }

}

5 个答案:

答案 0 :(得分:2)

似乎唯一的问题是我使用的是SQL Server 2008,并且必须在连接属性中放置sendTimeAsDateTime = false。

这是一个类似的问题。 comparing time in sql server through hibernate

答案 1 :(得分:0)

如果没有设置sendTimeAsDateTime属性,这对于早于3.0的SQL Server驱动程序不可用(并且我们中的一些人因为我们的原因而停滞不前),您可以尝试使用String而不是日期。这对我使用PreparedStatement有用,我打赌它也可以在这种情况下工作。将第一个代码块的最后一行更改为:

.setParameter( "hour", new SimpleDateFormat("HH:mm:ss.SSS").format(d3) ).list();

答案 2 :(得分:0)

通过javax.time.LocalDatespring-data-jpa按类型eclipselink的实体属性查询数据时,我遇到了同样的问题。连接设置sendTimeAsDateTime=false没有帮助。通过将spring-data-jpa转换器<class>org.springframework.data.jpa.convert.threeten.Jsr310JpaConverters$LocalDateConverter</class>添加到persistence.xml文件中,可以解决此问题。希望这会有所帮助。

答案 3 :(得分:0)

我也有同样的问题。我的sql数据类型是app.use(compress()) .options('*', cors()) .use(cors()) // favicon and serveStatic used to be here .use(bodyParser.json()) .use(bodyParser.urlencoded({ extended: true })) .configure(hooks()) .configure(rest()) .configure(socketio()) .configure(services) .use(require('connect-history-api-fallback')()) // now they are down here .use(favicon(path.join(app.get('public'), 'favicon.ico'))) .use('/', serveStatic(app.get('public'))) .configure(middleware); ,每次我想通过JPQL查询进行比较时,都会出现错误。连接字符串Time(7)无效。将sendTimeAsDateTime=false添加到<class>org.springframework.data.jpa.convert.threeten.Jsr310JpaConverters$LocalDateConverter</class>也无效。

我所做的是,将数据persistence.xml从sql存储到Time(7),例如这是我在sql上的表设计

String

在我的java类中,我将该字段存储到StartTime time(7) 变量

String

当我像这样使用JPQL查询时(在存储库中)

@Entity
@Table(name = "tableSchedule")
public class TableSchedulue implements Serializable {
    private static final long serialVersionUID = -9112498278775770919L;
    ....
    @Column(name = "StartTime")
    private String startTime;
    .....
}

您的字符串格式必须为@Query(value = "SELECT a " + "FROM TableSchedule a " + "WHERE a.startTime <= ?1 ") List<TableSchedulue > getTestData(String startTime);
有用。希望它有所帮助

缺点:因为在SQL中,类型为HH:mm:ss,因此SQL中的值Time(7)将在持久性中变为08:00:00,因此您需要将其解析为您的需求

答案 4 :(得分:0)

在调用传递空日期的JpaRepository API时,我使用SpringBoot(v2.2.2)和MSSQL服务器(jdbc7.4.1)遇到此错误。这是第一个版本库API

@Query(value = "SELECT t FROM MyEntity t WHERE "
 + " AND ( ?3 IS NULL OR ( ?3 IS NOT NULL AND t.fromDate<= ?3 ))"
 + " AND ( ?2 IS NULL OR ( ?2 IS NOT NULL AND t.toDate>= ?2 ))") 
List<MyEntity> getMyEntity(LocalDate fromDate, LocalDate toDate);

在输入日期时使用空值调用API时出现异常:

The data types date and varbinary are incompatible in the less than or equal to operator

我通过CAST解决了问题

@Query(value = "SELECT t FROM MyEntity t WHERE "
 + " AND ( ?3 IS NULL OR ( ?3 IS NOT NULL AND t.fromDate<= CAST( ?3 AS date ) ))"
 + " AND ( ?2 IS NULL OR ( ?2 IS NOT NULL AND t.toDate>= CAST( ?2 AS date ) ))")
List<MyEntity> getMyEntity(LocalDate fromDate, LocalDate toDate);