Example usage for org.hibernate.cfg NamingStrategy collectionTableName

List of usage examples for org.hibernate.cfg NamingStrategy collectionTableName

Introduction

In this page you can find the example usage for org.hibernate.cfg NamingStrategy collectionTableName.

Prototype

public String collectionTableName(String ownerEntity, String ownerEntityTable, String associatedEntity,
        String associatedEntityTable, String propertyName);

Source Link

Document

Return a collection table name ie an association having a join table

Usage

From source file:org.jboss.tools.hibernate.jpt.core.internal.context.NamingStrategyMappingTools.java

License:Open Source License

public static String buildJoinTableDefaultName(Relationship relationshipReference) {
    if (relationshipReference.getJpaProject().getDataSource().connectionProfileIsActive()) {
        return buildDbJoinTableDefaultName(relationshipReference);
    }/*from   w  ww  .j  a va2  s .co  m*/

    RelationshipMapping relationshipMapping = relationshipReference.getMapping();
    if (relationshipMapping == null) {
        return null;
    }

    if (!(relationshipReference.getTypeMapping() instanceof Entity))
        return null;//could be JavaNullTypeMapping

    Entity ownerEntity = (Entity) relationshipReference.getTypeMapping();
    org.eclipse.jpt.jpa.core.context.Table ownerTable = ownerEntity.getTable();
    if (ownerTable == null) {
        return null;
    }

    Entity targetEntity = relationshipMapping.getResolvedTargetEntity();
    if (targetEntity == null) {
        return null;
    }

    org.eclipse.jpt.jpa.core.context.Table targetTable = targetEntity.getTable();
    if (targetTable == null) {
        return null;
    }

    NamingStrategy ns = getJpaProject(relationshipReference).getNamingStrategy();
    if (getJpaProject(relationshipReference).isNamingStrategyEnabled() && ns != null) {
        /*
         * By testing generated DDL I have found for JPA console configuration:
         * 1) first parameter of the method is always fully qualified owner entity class name
         * 2) second and forth parameters of the method are always fully qualified target entity class name
         * 3) third parameter of the method is name attribute of @Table annotation,
         *       if it is not specified, then it is *unqualified* name attribute of @Entity annotation
         *       if @Entity annotation not specified it is *unqualified* name of the target entity class.
         * 4) fifth parameter is owner entity field name (even if @Column annotation set different name)
         * 
         */
        try {
            String targetEntityName = targetEntity.getPersistentType().getName();
            String ownerEntityName = ownerEntity.getPersistentType().getName();
            String propName = relationshipMapping.getPersistentAttribute().getName();
            return ns.collectionTableName(ownerEntityName, targetTable.getName(), targetEntityName,
                    targetTable.getName(), propName);
        } catch (Exception e) {
            IMessage m = HibernateJpaValidationMessage.buildMessage(IMessage.HIGH_SEVERITY,
                    Messages.NAMING_STRATEGY_EXCEPTION, relationshipReference);
            HibernateJptPlugin.logException(m.getText(), e);
        }
    }
    return ownerTable.getName() + '_' + targetTable.getName();
}