HQL相关子查询使用group by产生子查询返回多行MySQL

时间:2017-07-30 11:09:03

标签: java mysql hibernate hql

视频实体:

@Entity
@Table(name = "video")    
public class Video implements Serializable{
     ....
     @OneToMany(mappedBy = "video",fetch = FetchType.LAZY)
     @Getter
     @Setter
     private List<ActivityLog> activityLogs;
}

第二实体:

@Entity
@Table(name = "activity_log")
public class ActivityLog implments Serializable{

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    @Getter
    @Setter
    private Long id;

    @Column(name = "action")
    @Enumerated(EnumType.STRING)
    @Getter
    @Setter
    private ActivityLogEnum action;

    @Column(name = "ctime")
    @Temporal(TemporalType.TIMESTAMP)
    @Getter
    @Setter
    private Date ctime;
    @JoinColumn(name = "account", referencedColumnName = "id")
    @ManyToOne(fetch = FetchType.LAZY)
    @Getter
    @Setter
    private Account account;
    @JoinColumn(name = "video", referencedColumnName = "id")
    @ManyToOne(fetch = FetchType.LAZY)
    @Getter
    @Setter
    private Video video;
}

我想选择视频活动日志操作字段分组 所以我试过了:

select v,(select al from ActivityLog al where al.video.id=v.id group by al.action order by al.id desc) as al from Video v

然后面对相关子查询产生:Subquery returns more than 1 row MySQL

还试过left join

select v from Video v left join (select al from ActivityLog al where al.video.id=v.id group by al.id,al.action order by al.id desc)

同样的问题

还尝试了join fetch,但却陷入了如何通过

编写群组的问题

那么如何在Hibernate中实现呢

activity_log表:

enter image description here

动作值将是以下枚举中提到的3之一:

public enum ActivityLogEnum {

    TRANSLATION_SESSION_STARTED("Session Start Time"), TRANSLATION_APPROVAL("Approval Start Time"), TRANSLATION_REJECTION("Rejection Start Time");
    @Getter
    @Setter
    private String label;

    private ActivityLogEnum(String label) {
        this.label = label;
    }

}

我想通过动作desc进行分组,以便在最后一次取消时不会获取多个动作,可以获取3种类型的动作。

提前致谢

0 个答案:

没有答案