I have a problem with this.
Hibernate uses a org.hibernate.dialect.Dialect.SQLFunctionRegistrydatabase function for recognition.
Here is an example of a sleeping kernel 4.3.10. Internally, it consists of two private fields:
public class SQLFunctionRegistry {
private final Dialect dialect;
private final Map<String, SQLFunction> userFunctions;
The first field represents the dialect of the database. The second contains custom functions that can be populated org.hibernate.cfg.Configuration#addSqlFunction().
, , , hibernate, .
SQLFunctionRegistry.
EntityManagerFactory
@Autowired
private EntityManagerFactory emFactory;
:
private void registerMyDbFunctions()
{
SQLFunctionRegistry registry = this.emFactory.unwrap(org.hibernate.internal.SessionFactoryImpl.class).getSqlFunctionRegistry();
Field field = ReflectionUtils.findField(SQLFunctionRegistry.class, "userFunctions");
ReflectionUtils.makeAccessible(field);
Map<String, SQLFunction> userFunctions = (Map<String, SQLFunction>)ReflectionUtils.getField(field, registry);
userFunctions.put("my_func", new SQLFunctionTemplate(TextType.INSTANCE, "my_func(?1, ?2)"));
}
userFunctions , ReflectionUtils, . , DB.
SqlFunctionRegistry , , .