如何使用MySQL和Hibernate设置group_concat_max_len

前端之家收集整理的这篇文章主要介绍了如何使用MySQL和Hibernate设置group_concat_max_len前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

while using group concat in query I am not able to get all the event
group name due to default length of group concat is 1024 so how I can
set max_length of group concat in existing code.

我有一个代码,我在使用group concat并设置max len

==========================================================================
        DATA_QUERY="set group_concat_max_len=10024;
 select group_concat(eg.name) from event_groups eg left join theatres t ON t.theatre_id = eg.theatre_id group by t.theatre_id order by t.application_name"

        Session session = getFacadeLookup().getPersistenceFacade().getHibernateSession();
        Query query = session.createsqlQuery(DATA_QUERY) and execute 

        List

错误集group_concat_max_len不支持此处

最佳答案
首先尝试设置group_concat_max_len:

session.doWork(connection -> {
    try(Statement statement = connection.createStatement()) {
        statement.execute("SET GLOBAL group_concat_max_len=10024");
    }
});

或Java 8之前的语法:

session.doWork(new Work() {
    @Override
    public void execute(Connection connection) throws sqlException {
        try (Statement statement = connection.createStatement()) {
            statement.execute("SET GLOBAL group_concat_max_len=10024");
        }
    }
});

然后才执行您的查询

Query query = session.createsqlQuery(
    "select group_concat(eg.name) " +
    "from event_groups eg " +
    "left join theatres t ON t.theatre_id = eg.theatre_id " +
    "group by t.theatre_id order by t.application_name");
List

猜你在找的Spring相关文章