jpa(休眠)中具有重复行的结果集

Posted

技术标签:

【中文标题】jpa(休眠)中具有重复行的结果集【英文标题】:Result set whit duplicate rows in jpa (Hibernate) 【发布时间】:2016-12-01 04:33:38 【问题描述】:

我有下一个 sql 查询:

SELECT 
    @row_number:=@row_number+1 AS ID, IGN_DATES.PLACA, IGN_DATES.FECHA, OFF_DATES.FECHA
FROM
    (SELECT 
        a.PLACA AS PLACA, MIN(p.FECHA_GPS) AS FECHA
    FROM
        POSICIONHISTORIAL p
    JOIN AUTOMOVIL a ON p.SECAUTOMOVIL = a.SECAUTOMOVIL
    JOIN EVENTO e ON p.EVENTO_SECEVENTO = e.SECEVENTO
    WHERE
        p.FECHA_GPS BETWEEN '2016-09-01' AND '2016-10-01'
            AND e.CODIGO = 'ENC'
            AND a.PLACA IN ('SHS946', 'SHT011')
    GROUP BY a.PLACA , DAY(p.FECHA_GPS)) AS IGN_DATES
        JOIN
    (SELECT 
        a.PLACA AS PLACA,
            SUBSTRING_INDEX(GROUP_CONCAT(p.FECHA_GPS
                ORDER BY p.FECHA_GPS DESC
                SEPARATOR ' , '), ',', 2) AS FECHA
    FROM
        POSICIONHISTORIAL p
    JOIN AUTOMOVIL a ON p.SECAUTOMOVIL = a.SECAUTOMOVIL
    JOIN EVENTO e ON p.EVENTO_SECEVENTO = e.SECEVENTO
    WHERE
        p.FECHA_GPS BETWEEN '2016-09-01' AND '2016-10-01'
            AND e.CODIGO = 'APA'
            AND a.PLACA IN ('SHS946', 'SHT011')
    GROUP BY a.PLACA , DAY(p.FECHA_GPS)) 
AS OFF_DATES ON DATE(IGN_DATES.FECHA) = DATE(OFF_DATES.FECHA),
(SELECT @row_number:=0) AS t;

我用的是mysql 5.7,这个查询的结果是:

Result Set in Mysql

但是从 JPA 2.1 (Hibernate) 调用的本机查询返回 OFF_DATES.FECHA 的重复值,这是来自 eclipse 调试的值。

Result set in JPA

语句中的 rowid 是为了解决问题而聚合的,但这并不成功

谢谢你的帮助。

【问题讨论】:

One-To-Many relationship gets duplicate objects whithout using "distinct".Why?的可能重复 @AlanHay 感谢您的评论,但对我没有用,我发布了我的答案。 【参考方案1】:

我解决了这个问题,问题不是One-To-Many relationship gets duplicate objects whithout using "distinct".Why?。

问题是用于连接的列,我又添加了一列:

SELECT 
    @row_number:=@row_number+1 AS ID, IGN_DATES.PLACA, IGN_DATES.FECHA, OFF_DATES.FECHA
FROM
    (SELECT 
        a.PLACA AS PLACA, MIN(p.FECHA_GPS) AS FECHA
    FROM
        POSICIONHISTORIAL p
    JOIN AUTOMOVIL a ON p.SECAUTOMOVIL = a.SECAUTOMOVIL
    JOIN EVENTO e ON p.EVENTO_SECEVENTO = e.SECEVENTO
    WHERE
        p.FECHA_GPS BETWEEN '2016-09-01' AND '2016-10-01'
            AND e.CODIGO = 'ENC'
            AND a.PLACA IN ('SHS946', 'SHT011')
    GROUP BY a.PLACA , DAY(p.FECHA_GPS)) AS IGN_DATES
        JOIN
    (SELECT 
        a.PLACA AS PLACA,
            SUBSTRING_INDEX(GROUP_CONCAT(p.FECHA_GPS
                ORDER BY p.FECHA_GPS DESC
                SEPARATOR ' , '), ',', 2) AS FECHA
    FROM
        POSICIONHISTORIAL p
    JOIN AUTOMOVIL a ON p.SECAUTOMOVIL = a.SECAUTOMOVIL
    JOIN EVENTO e ON p.EVENTO_SECEVENTO = e.SECEVENTO
    WHERE
        p.FECHA_GPS BETWEEN '2016-09-01' AND '2016-10-01'
            AND e.CODIGO = 'APA'
            AND a.PLACA IN ('SHS946', 'SHT011')
    GROUP BY a.PLACA , DAY(p.FECHA_GPS)) 
AS OFF_DATES ON DATE(IGN_DATES.FECHA) = DATE(OFF_DATES.FECHA)
AND IGN_DATES.PLACA = OFF_DATES.PLACA,
(SELECT @row_number:=0) AS t;

【讨论】:

以上是关于jpa(休眠)中具有重复行的结果集的主要内容,如果未能解决你的问题,请参考以下文章

JPA:迭代大型结果集的正确模式是啥?

具有多个结果集的NHibernate存储过程[重复]

使用可滚动结果集在休眠中批量读取数据

jpa 结果集映射抛出 NPE

如何将数据集拆分为两个具有唯一和重复行的数据集?

如何将数据集拆分为两个具有唯一和重复行的数据集?