栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Spring数据JPA findByDate总是返回一个空列表

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring数据JPA findByDate总是返回一个空列表

您是否生成了数据库方案?您是否尝试生成它?

如果可以生成(带删除),请在application.properties中设置以下属性。

spring.jpa.hibernate.ddl-auto=create-drop

可能您的数据库中具有不同的数据类型(例如,日期与日期时间),并由spring数据使用。我已经重新创建了您的项目,并且在H2和MySQL上都可以正常工作。

编辑:

尝试更新您的列说明,如下所示:

@Column(name = "creation", columnDefinition="TIMESTAMP(6)")@Temporal(TemporalType.TIMESTAMP)private Date creation;

精度可能有所不同。看我的代码:

订单类。

@Entity@Table(name = "Ordr")public class Order implements Serializable {    private static final long serialVersionUID = 1L;    @Id    @GeneratedValue(strategy=GenerationType.AUTO)    private Long id;    @Column(name = "creation", columnDefinition="TIMESTAMP(6)")    @Temporal(TemporalType.TIMESTAMP)    private Date creation;    // getters and setters}

测试bean类。

@Componentpublic class SomeBean {    @Autowired    private OrderRepository orderRepository;    @PostConstruct    public void init() {        Date date = new Date();        Order order = new Order();        order.setId(1L);        order.setCreation(date);        orderRepository.save(order);        Date creationFromDB = orderRepository.findOne(1L).getCreation();        System.out.println("eq? : "+ new Date(creationFromDB.getTime()).equals(order.getCreation()));        List<Order> ods = orderRepository.findByCreation(creationFromDB);        List<Order> ods2 = orderRepository.findByCreation2(creationFromDB);        System.out.println("n ods.size: " + ods.size() +", ods2.size: "+ods2.size());    }}

结果:等于?:true ods.size:1,ods2.size:1

数据库查询:

mysql> select * from ordr;+----+----------------------------+| id | creation        |+----+----------------------------+|  1 | 2016-08-03 15:15:12.386000 |+----+----------------------------+1 row in set (0,00 sec)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/431532.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号