Search in sources :

Example 1 with Entity

use of org.eclipse.jpt.jpa.core.context.Entity in project jbosstools-hibernate by jbosstools.

the class HibernateJavaJoinColumnImpl method getReferencedPersistentAttribute.

@Override
public PersistentAttribute getReferencedPersistentAttribute() {
    if (this.parentAdapter.getJoinColumnsSize() != 1) {
        return null;
    }
    Entity targetEntity = this.parentAdapter.getRelationshipTarget();
    if (targetEntity == null) {
        return null;
    }
    PersistentAttribute pAttr = null;
    Iterator<PersistentAttribute> attributes = targetEntity.getPersistentType().getAllAttributes().iterator();
    for (Iterator<PersistentAttribute> stream = attributes; stream.hasNext(); ) {
        PersistentAttribute attribute = stream.next();
        String name = attribute.getPrimaryKeyColumnName();
        if (name != null) {
            if (pAttr == null) {
                pAttr = attribute;
            } else {
                return null;
            }
        }
    }
    return pAttr;
}
Also used : Entity(org.eclipse.jpt.jpa.core.context.Entity) PersistentAttribute(org.eclipse.jpt.jpa.core.context.PersistentAttribute)

Example 2 with Entity

use of org.eclipse.jpt.jpa.core.context.Entity in project jbosstools-hibernate by jbosstools.

the class NamingStrategyMappingTools method buildDbJoinTableDefaultName.

protected static String buildDbJoinTableDefaultName(Relationship relationshipReference) {
    Table owningTable = relationshipReference.getTypeMapping().getPrimaryDbTable();
    if (owningTable == null) {
        return null;
    }
    RelationshipMapping relationshipMapping = relationshipReference.getMapping();
    if (relationshipMapping == null) {
        return null;
    }
    Entity targetEntity = relationshipMapping.getResolvedTargetEntity();
    if (targetEntity == null) {
        return null;
    }
    Table targetTable = targetEntity.getPrimaryDbTable();
    if (targetTable == null) {
        return null;
    }
    String name = owningTable.getName() + '_' + targetTable.getName();
    return owningTable.getDatabase().convertNameToIdentifier(name);
}
Also used : Entity(org.eclipse.jpt.jpa.core.context.Entity) Table(org.eclipse.jpt.jpa.db.Table) RelationshipMapping(org.eclipse.jpt.jpa.core.context.RelationshipMapping)

Example 3 with Entity

use of org.eclipse.jpt.jpa.core.context.Entity in project jbosstools-hibernate by jbosstools.

the class NamingStrategyMappingTools method buildJoinColumnDefaultName.

public static String buildJoinColumnDefaultName(JoinColumn joinColumn, JoinColumn.ParentAdapter parent) {
    if (parent.getJoinColumnsSize() != 1) {
        return null;
    }
    String prefix = parent.getAttributeName();
    if (prefix == null) {
        Entity targetEntity = parent.getRelationshipTarget();
        if (targetEntity == null) {
            return null;
        }
        prefix = targetEntity.getName();
    }
    // not sure which of these is correct...
    // (the spec implies that the referenced column is always the
    // primary key column of the target entity)
    // Column targetColumn = joinColumn.getTargetPrimaryKeyDbColumn();
    String targetColumnName = joinColumn.getReferencedColumnName();
    if (targetColumnName == null) {
        return null;
    }
    String name = prefix + '_' + targetColumnName;
    // return targetColumn.getDatabase().convertNameToIdentifier(name);
    return name;
}
Also used : Entity(org.eclipse.jpt.jpa.core.context.Entity)

Example 4 with Entity

use of org.eclipse.jpt.jpa.core.context.Entity in project jbosstools-hibernate by jbosstools.

the class HibernateOrmJoinColumnImpl method getReferencedPersistentAttribute.

public PersistentAttribute getReferencedPersistentAttribute() {
    if (this.parentAdapter.getJoinColumnsSize() != 1) {
        return null;
    }
    Entity targetEntity = this.parentAdapter.getRelationshipTarget();
    if (targetEntity == null) {
        return null;
    }
    PersistentAttribute pAttr = null;
    for (PersistentAttribute attribute : targetEntity.getPersistentType().getAllAttributes()) {
        String name = attribute.getPrimaryKeyColumnName();
        if (name != null) {
            if (pAttr == null) {
                pAttr = attribute;
            } else {
                return null;
            }
        }
    }
    return pAttr;
}
Also used : Entity(org.eclipse.jpt.jpa.core.context.Entity) PersistentAttribute(org.eclipse.jpt.jpa.core.context.PersistentAttribute)

Example 5 with Entity

use of org.eclipse.jpt.jpa.core.context.Entity in project jbosstools-hibernate by jbosstools.

the class NamingStrategyMappingTools method buildJoinTableDefaultName.

public static String buildJoinTableDefaultName(Relationship relationshipReference) {
    if (relationshipReference.getJpaProject().getDataSource().connectionProfileIsActive()) {
        return buildDbJoinTableDefaultName(relationshipReference);
    }
    RelationshipMapping relationshipMapping = relationshipReference.getMapping();
    if (relationshipMapping == null) {
        return null;
    }
    if (!(relationshipReference.getTypeMapping() instanceof Entity))
        // could be JavaNullTypeMapping
        return null;
    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;
    }
    INamingStrategy 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();
}
Also used : Entity(org.eclipse.jpt.jpa.core.context.Entity) RelationshipMapping(org.eclipse.jpt.jpa.core.context.RelationshipMapping) IMessage(org.eclipse.wst.validation.internal.provisional.core.IMessage) INamingStrategy(org.jboss.tools.hibernate.runtime.spi.INamingStrategy)

Aggregations

Entity (org.eclipse.jpt.jpa.core.context.Entity)5 PersistentAttribute (org.eclipse.jpt.jpa.core.context.PersistentAttribute)2 RelationshipMapping (org.eclipse.jpt.jpa.core.context.RelationshipMapping)2 Table (org.eclipse.jpt.jpa.db.Table)1 IMessage (org.eclipse.wst.validation.internal.provisional.core.IMessage)1 INamingStrategy (org.jboss.tools.hibernate.runtime.spi.INamingStrategy)1