A note from 2026: This article was published in 2018. SAP Commerce Cloud has since phased out the “hybris” branding, HAC/Groovy-console and direct database/JMX operations are often restricted in cloud environments, and MySQL Connector/J moved from `com.mysql.jdbc` to `com.mysql.cj`. Treat these scripts as on-premise/private-cloud examples and validate internal APIs on your target release.

All these scripts are intended as templates for something specific to your particular project. They demonstrate that actions such as restarting the server are possible in Hybris via Groovy and show the general approach.

The first script is used to convert FlexibleSearch queries into raw SQL. Hybris HAC can convert it, but the result is not 100% ready: it contains question marks for the internal parameters, which HAC doesn’t show to the administrator.

The second script shows all Hybris types that use a particular type.

The third script helps you execute DDL queries on the database server remotely (such as ALTER TABLE or CREATE INDEX, for example). I also provide a script for printing the database table structure and the indexes defined for it.

PART 5

Restart a Hybris server from HAC

de.hybris.platform.jmx.JmxClient.restartWrapper(new File('../../../../data/hybristomcat.java.pid').text as Integer);
query="SELECT count(a.pk) AS itemcount, a.code AS itemtype FROM "+
"({{SELECT {i:pk} AS pk, {c:code} AS code FROM {Item AS i},"+
"{ComposedType AS c} WHERE {i:itemtype}={c:pk}}}) a GROUP BY a.code ORDER BY code";

import de.hybris.platform.servicelayer.search.FlexibleSearchQuery;
import de.hybris.platform.persistence.property.JDBCValueMappings;
import de.hybris.platform.core.Registry;
import com.mysql.jdbc.JDBC4PreparedStatement;
import de.hybris.platform.jdbcwrapper.PreparedStatementImpl;

flexibleSearchService = spring.getBean("flexibleSearchService");
flexibleSearchQuery = new FlexibleSearchQuery(query);
params = flexibleSearchService.translate(flexibleSearchQuery).getSQLQueryParameters();
sql = flexibleSearchService.translate(flexibleSearchQuery).getSQLQuery();
con = Registry.getMasterTenant().getDataSource().getConnection();
preparedStatement = con.prepareStatement(sql);
JDBCValueMappings.getInstance().fillStatement(preparedStatement, params);
stringRepresentationOfPreparedStatement=preparedStatement.getPrepStmtPassthruString();
sql = (stringRepresentationOfPreparedStatement =~ /^(.*?)\: (.*?)$/)[0][2]

All Hybris types that use a particular type

import de.hybris.platform.hac.data.dto.SqlSearchResultData;
import de.hybris.platform.hac.facade.impl.DefaultFlexibleSearchFacade;
import de.hybris.platform.core.PK;
TYPE="Media"
typeService = spring.getBean("typeService");
typePK = typeService.getTypeForCode(TYPE).getPk();

SqlSearchResultData searchResult;
query =
"select EnclosingTypePK, QualifierInternal from attributedescriptors where AttributeTypePK = '"+typePK + "'"
;
flexibleSearchFacade = new DefaultFlexibleSearchFacade();
result = flexibleSearchFacade.executeRawSql(query, 2000000, false);

for (item in result.getResultList()) {
enclosingTypePk = item[0];
enclosingType = modelService.get(new PK(enclosingTypePk as Long));
println (enclosingType.getCode() + "." + item[1]);
}

Executing SQL

import de.hybris.platform.core.Registry;
conn = Registry.getCurrentTenant().getDataSource().getConnection();
stmt = conn.createStatement();
alterTableQuery = "alter table...";
abc = stmt.executeUpdate(alterTableQuery)
import de.hybris.platform.core.Registry;
conn = Registry.getCurrentTenant().getDataSource().getConnection();
println conn
stmt = conn.createStatement();
query = "desc genericitems";
rs = stmt.executeQuery(query);
while (rs.next()) {
String field = rs.getString("Field");
String type = rs.getString("type");
String nullinfo = rs.getString("null");
String key = rs.getString("key");
String extra = rs.getString("extra");
println field+"\t"+type+"\t"+nullinfo+"\t"+key+"\t"+extra;
}
import de.hybris.platform.core.Registry;
conn = Registry.getCurrentTenant().getDataSource().getConnection();
println conn
stmt = conn.createStatement();
stmt2 = conn.createStatement();
query = "show tables";
rs = stmt.executeQuery(query);
while (rs.next()) {
table = rs.getString(1);
getIndexesQuery = "SHOW INDEX FROM "+table;
println getIndexesQuery
indexesResult = stmt2.executeQuery(getIndexesQuery);
while (indexesResult.next()) {
for (i=1; i<=indexesResult.getMetaData().getColumnCount();i++) {
print "\t"+indexesResult.getString(i);
}
println "";
}
}