DB connection for DDL in groovy

DANGER!!!

In hybris hac, you cannot run DDL (data definition language) commands. You need unprotected connection to db. You can use below example. DDL commands are dangerous!!!

import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

def config = spring.getBean("configurationService").getConfiguration();

DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(config.getString("db.driver"));
dataSource.setUrl(config.getString("db.url"));
dataSource.setUsername(config.getString("db.username"));
dataSource.setPassword(config.getString("db.password"));
JdbcTemplate jdbc = new JdbcTemplate(dataSource)
jdbc.execute("ALTER TABLE MYTABLE ALTER (P_MYFIELD BIGINT)");

Delete all item by type

You can delete all item by type with impex. Below example removing all stock levels.
REMOVE StockLevel[batchmode=true];itemType(code)[unique=true]
;StockLevel

Update object private property with groovy

You can update private field an object with below sample code.

def myBean = spring.getBean("myBean");

def f = myBean.getClass().getDeclaredField("myField");
f.setAccessible(true);
f.set(myBean, "new string value");

Update all items with impex

We canuse bactcmode for updating all items by type

UPDATE Product[batchmode=true];itemType(code)[unique=true];description[en][default='test']
;Product;
;VariantProduct;