Search in sources :

Example 21 with KeyValue

use of org.hibernate.mapping.KeyValue in project jbosstools-hibernate by jbosstools.

the class PersistentClassFacadeTest method testGetIdentifier.

@Test
public void testGetIdentifier() throws Exception {
    KeyValue valueTarget = createValue();
    Field field = AbstractPersistentClassFacade.class.getDeclaredField("identifier");
    field.setAccessible(true);
    assertNull(field.get(persistentClassFacade));
    assertNull(persistentClassFacade.getIdentifier());
    assertNull(field.get(persistentClassFacade));
    ((RootClass) persistentClassTarget).setIdentifier(valueTarget);
    IValue valueFacade = persistentClassFacade.getIdentifier();
    assertNotNull(valueFacade);
    assertSame(valueFacade, field.get(persistentClassFacade));
    assertSame(valueTarget, ((IFacade) valueFacade).getTarget());
}
Also used : RootClass(org.hibernate.mapping.RootClass) Field(java.lang.reflect.Field) KeyValue(org.hibernate.mapping.KeyValue) IValue(org.jboss.tools.hibernate.runtime.spi.IValue) Test(org.junit.jupiter.api.Test)

Example 22 with KeyValue

use of org.hibernate.mapping.KeyValue in project jbosstools-hibernate by jbosstools.

the class ValueFacadeTest method testGetKey.

@Test
public void testGetKey() {
    Map valueTarget = new Map(null, null);
    valueFacade = FACADE_FACTORY.createValue(valueTarget);
    assertNull(valueFacade.getKey());
    KeyValue keyValue = new SimpleValue(null);
    valueTarget.setKey(keyValue);
    assertSame(keyValue, ((IFacade) valueFacade.getKey()).getTarget());
}
Also used : KeyValue(org.hibernate.mapping.KeyValue) Map(org.hibernate.mapping.Map) SimpleValue(org.hibernate.mapping.SimpleValue) Test(org.junit.jupiter.api.Test)

Example 23 with KeyValue

use of org.hibernate.mapping.KeyValue in project jbosstools-hibernate by jbosstools.

the class ValueFacadeTest method testSetKey.

@Test
public void testSetKey() {
    KeyValue keyValueTarget = new SimpleValue(null);
    IValue keyValueFacade = FACADE_FACTORY.createValue(keyValueTarget);
    Collection collectionTarget = new Bag(null, null);
    IValue collectionFacade = FACADE_FACTORY.createValue(collectionTarget);
    assertNull(collectionTarget.getKey());
    collectionFacade.setKey(keyValueFacade);
    assertSame(keyValueTarget, collectionTarget.getKey());
}
Also used : KeyValue(org.hibernate.mapping.KeyValue) IValue(org.jboss.tools.hibernate.runtime.spi.IValue) Collection(org.hibernate.mapping.Collection) Bag(org.hibernate.mapping.Bag) IdentifierBag(org.hibernate.mapping.IdentifierBag) SimpleValue(org.hibernate.mapping.SimpleValue) Test(org.junit.jupiter.api.Test)

Example 24 with KeyValue

use of org.hibernate.mapping.KeyValue in project jbosstools-hibernate by jbosstools.

the class ValueFacadeTest method testSetKey.

@Test
public void testSetKey() {
    KeyValue keyValueTarget = new SimpleValue(DummyMetadataBuildingContext.INSTANCE, null);
    IValue keyValueFacade = FACADE_FACTORY.createValue(keyValueTarget);
    Collection collectionTarget = new Bag(DummyMetadataBuildingContext.INSTANCE, null);
    IValue collectionFacade = FACADE_FACTORY.createValue(collectionTarget);
    assertNull(collectionTarget.getKey());
    collectionFacade.setKey(keyValueFacade);
    assertSame(keyValueTarget, collectionTarget.getKey());
}
Also used : KeyValue(org.hibernate.mapping.KeyValue) IValue(org.jboss.tools.hibernate.runtime.spi.IValue) Collection(org.hibernate.mapping.Collection) Bag(org.hibernate.mapping.Bag) IdentifierBag(org.hibernate.mapping.IdentifierBag) SimpleValue(org.hibernate.mapping.SimpleValue) Test(org.junit.jupiter.api.Test)

Example 25 with KeyValue

use of org.hibernate.mapping.KeyValue in project hibernate-orm by hibernate.

the class AnnotationBinder method bindOneToOne.

private static void bindOneToOne(String cascadeStrategy, Ejb3JoinColumn[] joinColumns, boolean optional, FetchMode fetchMode, boolean ignoreNotFound, boolean cascadeOnDelete, XClass targetEntity, PropertyHolder propertyHolder, PropertyData inferredData, String mappedBy, boolean trueOneToOne, boolean isIdentifierMapper, boolean inSecondPass, PropertyBinder propertyBinder, MetadataBuildingContext context) {
    // column.getTable() => persistentClass.getTable()
    final String propertyName = inferredData.getPropertyName();
    LOG.tracev("Fetching {0} with {1}", propertyName, fetchMode);
    boolean mapToPK = true;
    if (!trueOneToOne) {
        // try to find a hidden true one to one (FK == PK columns)
        KeyValue identifier = propertyHolder.getIdentifier();
        if (identifier == null) {
            // this is a @OneToOne in a @EmbeddedId (the persistentClass.identifier is not set yet, it's being built)
            // by definition the PK cannot refers to itself so it cannot map to itself
            mapToPK = false;
        } else {
            Iterator idColumns = identifier.getColumnIterator();
            List<String> idColumnNames = new ArrayList<>();
            org.hibernate.mapping.Column currentColumn;
            if (identifier.getColumnSpan() != joinColumns.length) {
                mapToPK = false;
            } else {
                while (idColumns.hasNext()) {
                    currentColumn = (org.hibernate.mapping.Column) idColumns.next();
                    idColumnNames.add(currentColumn.getName());
                }
                for (Ejb3JoinColumn col : joinColumns) {
                    if (!idColumnNames.contains(col.getMappingColumn().getName())) {
                        mapToPK = false;
                        break;
                    }
                }
            }
        }
    }
    if (trueOneToOne || mapToPK || !BinderHelper.isEmptyAnnotationValue(mappedBy)) {
        // is a true one-to-one
        // FIXME referencedColumnName ignored => ordering may fail.
        OneToOneSecondPass secondPass = new OneToOneSecondPass(mappedBy, propertyHolder.getEntityName(), propertyName, propertyHolder, inferredData, targetEntity, ignoreNotFound, cascadeOnDelete, optional, cascadeStrategy, joinColumns, context);
        if (inSecondPass) {
            secondPass.doSecondPass(context.getMetadataCollector().getEntityBindingMap());
        } else {
            context.getMetadataCollector().addSecondPass(secondPass, BinderHelper.isEmptyAnnotationValue(mappedBy));
        }
    } else {
        // has a FK on the table
        bindManyToOne(cascadeStrategy, joinColumns, optional, ignoreNotFound, cascadeOnDelete, targetEntity, propertyHolder, inferredData, true, isIdentifierMapper, inSecondPass, propertyBinder, context);
    }
}
Also used : KeyValue(org.hibernate.mapping.KeyValue) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList)

Aggregations

KeyValue (org.hibernate.mapping.KeyValue)53 Test (org.junit.jupiter.api.Test)44 IValue (org.jboss.tools.hibernate.runtime.spi.IValue)35 SimpleValue (org.hibernate.mapping.SimpleValue)33 Collection (org.hibernate.mapping.Collection)13 Bag (org.hibernate.mapping.Bag)12 IdentifierBag (org.hibernate.mapping.IdentifierBag)12 Map (org.hibernate.mapping.Map)12 Table (org.hibernate.mapping.Table)12 ITable (org.jboss.tools.hibernate.runtime.spi.ITable)12 Field (java.lang.reflect.Field)11 RootClass (org.hibernate.mapping.RootClass)11 Iterator (java.util.Iterator)3 BasicValue (org.hibernate.mapping.BasicValue)3 Test (org.junit.Test)3 JoinColumn (javax.persistence.JoinColumn)2 Ejb3JoinColumn (org.hibernate.cfg.Ejb3JoinColumn)2 Property (org.hibernate.mapping.Property)2 AbstractValueFacade (org.jboss.tools.hibernate.runtime.common.AbstractValueFacade)2 ArrayList (java.util.ArrayList)1