【问题标题】:Spring data JPA with rest: how to filter out a subset of records in a one-to-many mappingSpring data JPA with rest:如何过滤掉一对多映射中的记录子集
【发布时间】:2017-03-19 05:26:16
【问题描述】:

我是一个糟糕的Java程序员...... 也许我以错误的方式设计我的数据库。欢迎提供任何线索。

现在是这样的: table1 命名节点具有: ID 信息 更新日期

名为 node_records 的 table2 具有: ID node_id(反向引用节点) 价值 创建日期

我想选择 node_records 中 node_record.node_id = node.id 和 node_record.create-date = node.update-date 的所有记录。

在 Node.java 中:

@OneToMany(targetEntity = NodeRecord.class, mappedBy = "node")
List<NodeRecord> nodeRecord;

在 NodeRecord.java 中:

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="id")
Node node;

现在,我可以选择 record.node_id = node.id 的所有记录,但我搜索了几个小时试图找出如何选择也满足条件 record.creat-date = node.updated-date 的记录。

【问题讨论】:

    标签: java sql spring jpa


    【解决方案1】:
    【解决方案2】:

    方言可以在这里提供帮助,有支持特定方言的数据库特定日期功能,例如: 如果您的数据库是 mysql,那么您可以在查询中使用“日期”函数

    Query query = session.createQuery("from NodeRecord  as nr join nr.node as n where date(nr.createdDate) = date(n.updateDate)");
    

    如果您使用的是 oracle,则可以在查询中使用 TRUNC 函数。

    【讨论】:

    • 您的查询返回Node的列表。
    猜你喜欢
    • 2022-12-15
    • 1970-01-01
    • 1970-01-01
    • 2020-11-30
    • 2020-07-10
    • 2021-09-15
    • 2020-04-20
    • 1970-01-01
    • 2019-03-27
    相关资源
    最近更新 更多