Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set group_concat_max_len with MySQL and Hibernate

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.

I have a code here were I am using group concat and set 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<Object[]> lstResult = query.list();
============================================================================

error set group_concat_max_len not support here

like image 878
Ravi Bhalsod Avatar asked Apr 21 '15 13:04

Ravi Bhalsod


1 Answers

Try setting the group_concat_max_len first:

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

or pre-Java 8 syntax:

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");
        }
    }
});

And only then execute your query:

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<Object[]> lstResult = query.list();
like image 85
Vlad Mihalcea Avatar answered Nov 13 '22 11:11

Vlad Mihalcea