org.jlot.hibernate.orm.JlotImplicitNamingStrategy Maven / Gradle / Ivy
package org.jlot.hibernate.orm;
import java.lang.reflect.Field;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.ImplicitBasicColumnNameSource;
import org.hibernate.boot.model.naming.ImplicitJoinColumnNameSource;
import org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl;
import org.hibernate.cfg.Ejb3Column;
public class JlotImplicitNamingStrategy extends ImplicitNamingStrategyComponentPathImpl
{
@Override
public Identifier determineJoinColumnName ( ImplicitJoinColumnNameSource source )
{
String name = source.getReferencedColumnName().toString();
Identifier identifier = toIdentifier(name, source.getBuildingContext());
return identifier;
}
@Override
public Identifier determineBasicColumnName ( ImplicitBasicColumnNameSource source )
{
try
{
Field ejb3ColumnField = source.getClass().getDeclaredField("this$0");
ejb3ColumnField.setAccessible(true);
Ejb3Column ejb3Column = (Ejb3Column) ejb3ColumnField.get(source);
// explicit naming oder implicit
String tableName = ejb3Column.getPropertyHolder().getTable().getName();
final Identifier basicColumnName = super.determineBasicColumnName(source);
String columnName = tableName + "_" + basicColumnName.toString();
return Identifier.toIdentifier(columnName);
}
catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e)
{
throw new RuntimeException(e);
}
}
}