How to override Hibernate ORM strategies for the Content Service
Atoti uses Hibernate to persist the state of the content server.
Each file of the content server represents an entry in the dedicated database table.
You may want to change some of the default Hibernate settings to better suit your needs.
Let's say you are using the in memory H2 database to store your files. You may want to change the type of the column which is mapped to the content of a file.
An entry in the content server is represented by the class CSEntry
and has an attribute called content
which holds te file content. The content
field has @LOB
annotation, therefore, it is mapped by Hibernate to a CLOB
column in Oracle DB. For some reason, you may want to change the mapping and store the content in a VARCHAR
column.
Here are two ways to achieve that:
Extending the database dialect
This method is more robust than the previous one. It allows you to change the type of the column without depending on Atoti implementation.
-
Create a custom dialect
H2CustomContentServiceDialect.java
import org.hibernate.dialect.H2Dialect;
import org.hibernate.type.SqlTypes;
public class H2CustomContentServiceDialect extends H2Dialect {
@Override
protected String columnType(int sqlTypeCode) {
final String columnType = super.columnType(sqlTypeCode);
final String clobColumnType = super.columnType(SqlTypes.CLOB);
if (columnType.equals(clobColumnType)) {
return super.columnType(SqlTypes.VARCHAR);
} else {
return columnType;
}
}
} -
Use the custom dialect
Add the custom dialect to the configuration properties of the content service.
final String databaseName = "content_service-" + UUID.randomUUID();
final Properties hibernateProperties = new Properties();
hibernateProperties.put(AvailableSettings.HBM2DDL_AUTO, "create-drop");
hibernateProperties.put(AvailableSettings.JAKARTA_JDBC_DRIVER, "org.h2.Driver");
hibernateProperties.put(AvailableSettings.JAKARTA_JDBC_URL, "jdbc:h2:mem:" + databaseName);
final Configuration configuration = new Configuration();
configuration.addProperties(hibernateProperties);
hibernateProperties.put(
AvailableSettings.DIALECT,
"com.activeviam.tech.contentserver.storage.private_.H2CustomContentServiceDialect");
configuration.addProperties(hibernateProperties);
Using an XML mapping file
This method is sensitive to changes in the Atoti. It requires to specifically know which class to override.
-
Create an XML file
content-service-hibernate.xml
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" version="2.0">
<entity class="com.activeviam.tech.contentserver.storage.private_.CSEntry">
<attribute-override name="content">
<column column-definition="varchar"/>
</attribute-override>
</entity>
</entity-mappings> -
Use the XML mapping
Create the content service with the custom hibernate configuration:
final String databaseName = "content_service-" + UUID.randomUUID();
final Properties hibernateProperties = new Properties();
hibernateProperties.put(AvailableSettings.HBM2DDL_AUTO, "create-drop");
hibernateProperties.put(AvailableSettings.JAKARTA_JDBC_DRIVER, "org.h2.Driver");
hibernateProperties.put(AvailableSettings.JAKARTA_JDBC_URL, "jdbc:h2:mem:" + databaseName);
final Configuration configuration = new Configuration();
configuration.addProperties(hibernateProperties);
final Resource entityMappingFile = new ClassPathResource("content-service-hibernate.xml");
configuration.addInputStream(entityMappingFile.getInputStream());Now the column should be of type of
VARCHAR
instead of aCLOB
. This can be used to fulfill other needs, like a bigger size for some column or to improve performance (see PostgreSQL tips).