Search in sources :

Example 1 with ForeignKey

use of javax.persistence.ForeignKey in project hibernate-orm by hibernate.

the class AbstractPropertyHolder method buildHierarchyColumnOverride.

private void buildHierarchyColumnOverride(XClass element) {
    XClass current = element;
    Map<String, Column[]> columnOverride = new HashMap<String, Column[]>();
    Map<String, JoinColumn[]> joinColumnOverride = new HashMap<String, JoinColumn[]>();
    Map<String, JoinTable> joinTableOverride = new HashMap<String, JoinTable>();
    Map<String, ForeignKey> foreignKeyOverride = new HashMap<String, ForeignKey>();
    while (current != null && !context.getBuildingOptions().getReflectionManager().toXClass(Object.class).equals(current)) {
        if (current.isAnnotationPresent(Entity.class) || current.isAnnotationPresent(MappedSuperclass.class) || current.isAnnotationPresent(Embeddable.class)) {
            //FIXME is embeddable override?
            Map<String, Column[]> currentOverride = buildColumnOverride(current, getPath());
            Map<String, JoinColumn[]> currentJoinOverride = buildJoinColumnOverride(current, getPath());
            Map<String, JoinTable> currentJoinTableOverride = buildJoinTableOverride(current, getPath());
            Map<String, ForeignKey> currentForeignKeyOverride = buildForeignKeyOverride(current, getPath());
            //subclasses have precedence over superclasses
            currentOverride.putAll(columnOverride);
            //subclasses have precedence over superclasses
            currentJoinOverride.putAll(joinColumnOverride);
            //subclasses have precedence over superclasses
            currentJoinTableOverride.putAll(joinTableOverride);
            //subclasses have precedence over superclasses
            currentForeignKeyOverride.putAll(foreignKeyOverride);
            columnOverride = currentOverride;
            joinColumnOverride = currentJoinOverride;
            joinTableOverride = currentJoinTableOverride;
            foreignKeyOverride = currentForeignKeyOverride;
        }
        current = current.getSuperclass();
    }
    holderColumnOverride = columnOverride.size() > 0 ? columnOverride : null;
    holderJoinColumnOverride = joinColumnOverride.size() > 0 ? joinColumnOverride : null;
    holderJoinTableOverride = joinTableOverride.size() > 0 ? joinTableOverride : null;
    holderForeignKeyOverride = foreignKeyOverride.size() > 0 ? foreignKeyOverride : null;
}
Also used : HashMap(java.util.HashMap) ForeignKey(javax.persistence.ForeignKey) XClass(org.hibernate.annotations.common.reflection.XClass) Embeddable(javax.persistence.Embeddable) JoinColumn(javax.persistence.JoinColumn) JoinColumn(javax.persistence.JoinColumn) Column(javax.persistence.Column) JoinTable(javax.persistence.JoinTable)

Example 2 with ForeignKey

use of javax.persistence.ForeignKey in project hibernate-orm by hibernate.

the class LongKeyNamingStrategyTest method testWithCustomNamingStrategy.

@Test
public void testWithCustomNamingStrategy() throws Exception {
    Metadata metadata = new MetadataSources(serviceRegistry).addAnnotatedClass(Address.class).addAnnotatedClass(Person.class).getMetadataBuilder().applyImplicitNamingStrategy(new LongIdentifierNamingStrategy()).build();
    org.hibernate.mapping.ForeignKey foreignKey = (org.hibernate.mapping.ForeignKey) metadata.getEntityBinding(Address.class.getName()).getTable().getForeignKeyIterator().next();
    assertEquals("FK_way_longer_than_the_30_char", foreignKey.getName());
    UniqueKey uniqueKey = metadata.getEntityBinding(Address.class.getName()).getTable().getUniqueKeyIterator().next();
    assertEquals("UK_way_longer_than_the_30_char", uniqueKey.getName());
    org.hibernate.mapping.Index index = metadata.getEntityBinding(Address.class.getName()).getTable().getIndexIterator().next();
    assertEquals("IDX_way_longer_than_the_30_cha", index.getName());
}
Also used : UniqueKey(org.hibernate.mapping.UniqueKey) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) ForeignKey(javax.persistence.ForeignKey) Test(org.junit.Test)

Example 3 with ForeignKey

use of javax.persistence.ForeignKey in project hibernate-orm by hibernate.

the class ForeignKeyConstraintTest method assertForeignKey.

private void assertForeignKey(String foreignKeyName, String... columns) {
    Set<String> columnSet = new LinkedHashSet<>(Arrays.asList(columns));
    for (Namespace namespace : metadata().getDatabase().getNamespaces()) {
        for (org.hibernate.mapping.Table table : namespace.getTables()) {
            Iterator<org.hibernate.mapping.ForeignKey> fkItr = table.getForeignKeyIterator();
            while (fkItr.hasNext()) {
                org.hibernate.mapping.ForeignKey fk = fkItr.next();
                if (foreignKeyName.equals(fk.getName())) {
                    assertEquals("ForeignKey column count not like expected", columnSet.size(), fk.getColumnSpan());
                    List<String> columnNames = fk.getColumns().stream().map(Column::getName).collect(Collectors.toList());
                    assertTrue("ForeignKey columns [" + columnNames + "] do not match expected columns [" + columnSet + "]", columnSet.containsAll(columnNames));
                    return;
                }
            }
        }
    }
    fail("ForeignKey '" + foreignKeyName + "' could not be found!");
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ForeignKey(javax.persistence.ForeignKey) Namespace(org.hibernate.boot.model.relational.Namespace)

Example 4 with ForeignKey

use of javax.persistence.ForeignKey in project hibernate-orm by hibernate.

the class ForeignKeyConstraintTest method assertNoForeignKey.

private void assertNoForeignKey(String foreignKeyName, String... columns) {
    Set<String> columnSet = new LinkedHashSet<>(Arrays.asList(columns));
    for (Namespace namespace : metadata().getDatabase().getNamespaces()) {
        for (org.hibernate.mapping.Table table : namespace.getTables()) {
            Iterator<org.hibernate.mapping.ForeignKey> fkItr = table.getForeignKeyIterator();
            while (fkItr.hasNext()) {
                org.hibernate.mapping.ForeignKey fk = fkItr.next();
                assertFalse("ForeignKey [" + foreignKeyName + "] defined and shouldn't have been.", foreignKeyName.equals(fk.getName()));
            }
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ForeignKey(javax.persistence.ForeignKey) Namespace(org.hibernate.boot.model.relational.Namespace)

Aggregations

ForeignKey (javax.persistence.ForeignKey)4 LinkedHashSet (java.util.LinkedHashSet)2 Namespace (org.hibernate.boot.model.relational.Namespace)2 HashMap (java.util.HashMap)1 Column (javax.persistence.Column)1 Embeddable (javax.persistence.Embeddable)1 JoinColumn (javax.persistence.JoinColumn)1 JoinTable (javax.persistence.JoinTable)1 XClass (org.hibernate.annotations.common.reflection.XClass)1 Metadata (org.hibernate.boot.Metadata)1 MetadataSources (org.hibernate.boot.MetadataSources)1 UniqueKey (org.hibernate.mapping.UniqueKey)1 Test (org.junit.Test)1