The data types datetime and time are incompatible in the greater than or equal to operator

青春壹個敷衍的年華 提交于 2019-12-05 23:04:50
oshingc

It seems the only problem was I'm using SQL Server 2008 and is necessary to put sendTimeAsDateTime=false in the connections properties.

Here a similar question. comparing time in sql server through hibernate

Without setting that sendTimeAsDateTime property, which is not available for the SQL Server drivers older than 3.0 (and some of us are stuck with what we have, for reasons), you could try to use a String instead of a date. This worked for me using a PreparedStatement and I bet it would work in this scenario also. Change the last line of your first code block to:

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

I've encountered the same issue when querying data by entity property of type javax.time.LocalDate via spring-data-jpa and eclipselink . Connection setting sendTimeAsDateTime=false didn't help. This was fixed by adding spring-data-jpa converter <class>org.springframework.data.jpa.convert.threeten.Jsr310JpaConverters$LocalDateConverter</class> into the persistence.xml file. Hope this helps.

I also have the same issue. My sql data type is Time(7) and each time I want to compare it via JPQL query, that error comes out. Connection string sendTimeAsDateTime=false didn't work. Adding <class>org.springframework.data.jpa.convert.threeten.Jsr310JpaConverters$LocalDateConverter</class> into the persistence.xml also didn't work.

What I do is, store data Time(7) from sql into String, for example this is my table design on sql

StartTime   time(7) 

In my java class I store that field into String variable

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

When I use JPQL query like this (in repository)

@Query(value = "SELECT a "
            + "FROM TableSchedule a "
            + "WHERE a.startTime <= ?1 ")
    List<TableSchedulue > getTestData(String startTime);

Your string format must HH:mm:ss
It works. Hope it helps

CONS : Because in SQL the type is Time(7) so value 08:00:00 in SQL will become 08:00:00.0000000 in persistence so you need to parse it into your needs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!