Example usage for org.hibernate.mapping OneToOne OneToOne

List of usage examples for org.hibernate.mapping OneToOne OneToOne

Introduction

In this page you can find the example usage for org.hibernate.mapping OneToOne OneToOne.

Prototype

public OneToOne(MetadataBuildingContext buildingContext, Table table, PersistentClass owner)
            throws MappingException 

Source Link

Usage

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java

License:Apache License

/**
 * Creates and binds the properties for the specified Grails domain class and PersistentClass
 * and binds them to the Hibernate runtime meta model
 *
 * @param domainClass     The Grails domain class
 * @param persistentClass The Hibernate PersistentClass instance
 * @param mappings        The Hibernate Mappings instance
 * @param sessionFactoryBeanName  the session factory bean name
 *//*w  ww . ja v  a2  s . c  o  m*/
protected void createClassProperties(GrailsDomainClass domainClass, PersistentClass persistentClass,
        Mappings mappings, String sessionFactoryBeanName) {

    GrailsDomainClassProperty[] persistentProperties = domainClass.getPersistentProperties();
    Table table = persistentClass.getTable();

    Mapping gormMapping = getMapping(domainClass.getClazz());

    if (gormMapping != null) {
        table.setComment(gormMapping.getComment());
    }

    for (GrailsDomainClassProperty currentGrailsProp : persistentProperties) {

        // if its inherited skip
        boolean isBidirectionalManyToOne = isBidirectionalManyToOne(currentGrailsProp);
        if (currentGrailsProp.isInherited()) {
            continue;
        }
        if (currentGrailsProp.isInherited() && isBidirectionalManyToOne && currentGrailsProp.isCircular()) {
            continue;
        }
        if (isCompositeIdProperty(gormMapping, currentGrailsProp))
            continue;
        if (isIdentityProperty(gormMapping, currentGrailsProp))
            continue;

        if (LOG.isDebugEnabled()) {
            LOG.debug("[GrailsDomainBinder] Binding persistent property [" + currentGrailsProp.getName() + "]");
        }

        Value value = null;

        // see if it's a collection type
        CollectionType collectionType = CT.collectionTypeForClass(currentGrailsProp.getType());

        Class<?> userType = getUserType(currentGrailsProp);

        if (userType != null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName()
                        + "] as SimpleValue");
            }
            value = new SimpleValue(mappings, table);
            bindSimpleValue(currentGrailsProp, null, (SimpleValue) value, EMPTY_PATH, mappings,
                    sessionFactoryBeanName);
        } else if (collectionType != null) {
            String typeName = getTypeName(currentGrailsProp, getPropertyConfig(currentGrailsProp), gormMapping);
            if ("serializable".equals(typeName)) {
                value = new SimpleValue(mappings, table);
                bindSimpleValue(typeName, (SimpleValue) value, currentGrailsProp.isOptional(),
                        getColumnNameForPropertyAndPath(currentGrailsProp, EMPTY_PATH, null,
                                sessionFactoryBeanName),
                        mappings);
            } else {
                // create collection
                Collection collection = collectionType.create(currentGrailsProp, persistentClass, EMPTY_PATH,
                        mappings, sessionFactoryBeanName);
                mappings.addCollection(collection);
                value = collection;
            }
        } else if (currentGrailsProp.isEnum()) {
            value = new SimpleValue(mappings, table);
            bindEnumType(currentGrailsProp, (SimpleValue) value, EMPTY_PATH, sessionFactoryBeanName);
        }
        // work out what type of relationship it is and bind value
        else if (currentGrailsProp.isManyToOne()) {
            if (LOG.isDebugEnabled())
                LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName()
                        + "] as ManyToOne");

            value = new ManyToOne(mappings, table);
            bindManyToOne(currentGrailsProp, (ManyToOne) value, EMPTY_PATH, mappings, sessionFactoryBeanName);
        } else if (currentGrailsProp.isOneToOne() && userType == null) {
            if (LOG.isDebugEnabled())
                LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName()
                        + "] as OneToOne");

            if (currentGrailsProp.isHasOne() && !currentGrailsProp.isBidirectional()) {
                throw new MappingException("hasOne property [" + currentGrailsProp.getDomainClass().getName()
                        + "." + currentGrailsProp.getName()
                        + "] is not bidirectional. Specify the other side of the relationship!");
            } else if (canBindOneToOneWithSingleColumnAndForeignKey(currentGrailsProp)) {
                value = new OneToOne(mappings, table, persistentClass);
                bindOneToOne(currentGrailsProp, (OneToOne) value, EMPTY_PATH, sessionFactoryBeanName);
            } else {
                if (currentGrailsProp.isHasOne() && currentGrailsProp.isBidirectional()) {
                    value = new OneToOne(mappings, table, persistentClass);
                    bindOneToOne(currentGrailsProp, (OneToOne) value, EMPTY_PATH, sessionFactoryBeanName);
                } else {
                    value = new ManyToOne(mappings, table);
                    bindManyToOne(currentGrailsProp, (ManyToOne) value, EMPTY_PATH, mappings,
                            sessionFactoryBeanName);
                }
            }
        } else if (currentGrailsProp.isEmbedded()) {
            value = new Component(mappings, persistentClass);

            bindComponent((Component) value, currentGrailsProp, true, mappings, sessionFactoryBeanName);
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName()
                        + "] as SimpleValue");
            }
            value = new SimpleValue(mappings, table);
            bindSimpleValue(currentGrailsProp, null, (SimpleValue) value, EMPTY_PATH, mappings,
                    sessionFactoryBeanName);
        }

        if (value != null) {
            Property property = createProperty(value, persistentClass, currentGrailsProp, mappings);
            persistentClass.addProperty(property);
        }
    }

    bindNaturalIdentifier(table, gormMapping, persistentClass);
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java

License:Apache License

protected void bindComponentProperty(Component component, GrailsDomainClassProperty componentProperty,
        GrailsDomainClassProperty currentGrailsProp, PersistentClass persistentClass, String path, Table table,
        Mappings mappings, String sessionFactoryBeanName) {
    Value value;/*from   w w w.  j  a  va  2  s  .  c om*/
    // see if it's a collection type
    CollectionType collectionType = CT.collectionTypeForClass(currentGrailsProp.getType());
    if (collectionType != null) {
        // create collection
        Collection collection = collectionType.create(currentGrailsProp, persistentClass, path, mappings,
                sessionFactoryBeanName);
        mappings.addCollection(collection);
        value = collection;
    }
    // work out what type of relationship it is and bind value
    else if (currentGrailsProp.isManyToOne()) {
        if (LOG.isDebugEnabled())
            LOG.debug(
                    "[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName() + "] as ManyToOne");

        value = new ManyToOne(mappings, table);
        bindManyToOne(currentGrailsProp, (ManyToOne) value, path, mappings, sessionFactoryBeanName);
    } else if (currentGrailsProp.isOneToOne()) {
        if (LOG.isDebugEnabled())
            LOG.debug(
                    "[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName() + "] as OneToOne");

        if (canBindOneToOneWithSingleColumnAndForeignKey(currentGrailsProp)) {
            value = new OneToOne(mappings, table, persistentClass);
            bindOneToOne(currentGrailsProp, (OneToOne) value, path, sessionFactoryBeanName);
        } else {
            value = new ManyToOne(mappings, table);
            bindManyToOne(currentGrailsProp, (ManyToOne) value, path, mappings, sessionFactoryBeanName);
        }
    } else if (currentGrailsProp.isEmbedded()) {
        value = new Component(mappings, persistentClass);
        bindComponent((Component) value, currentGrailsProp, true, mappings, sessionFactoryBeanName);
    } else {
        if (LOG.isDebugEnabled())
            LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName()
                    + "] as SimpleValue");

        value = new SimpleValue(mappings, table);
        if (currentGrailsProp.isEnum()) {
            bindEnumType(currentGrailsProp, (SimpleValue) value, path, sessionFactoryBeanName);
        } else {
            bindSimpleValue(currentGrailsProp, componentProperty, (SimpleValue) value, path, mappings,
                    sessionFactoryBeanName);
        }
    }

    if (value != null) {
        Property persistentProperty = createProperty(value, persistentClass, currentGrailsProp, mappings);
        component.addProperty(persistentProperty);
        if (isComponentPropertyNullable(componentProperty)) {
            final Iterator<?> columnIterator = value.getColumnIterator();
            while (columnIterator.hasNext()) {
                Column c = (Column) columnIterator.next();
                c.setNullable(true);
            }
        }
    }
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder.java

License:Apache License

/**
 * Creates and binds the properties for the specified Grails domain class and PersistentClass
 * and binds them to the Hibernate runtime meta model
 *
 * @param domainClass     The Grails domain class
 * @param persistentClass The Hibernate PersistentClass instance
 * @param mappings        The Hibernate Mappings instance
 * @param sessionFactoryBeanName  the session factory bean name
 *///w  w  w.  j  a  va 2  s . c  o  m
protected static void createClassProperties(GrailsDomainClass domainClass, PersistentClass persistentClass,
        Mappings mappings, String sessionFactoryBeanName) {

    GrailsDomainClassProperty[] persistentProperties = domainClass.getPersistentProperties();
    Table table = persistentClass.getTable();

    Mapping gormMapping = getMapping(domainClass.getClazz());

    for (GrailsDomainClassProperty currentGrailsProp : persistentProperties) {

        // if its inherited skip
        boolean isBidirectionalManyToOne = isBidirectionalManyToOne(currentGrailsProp);
        if (currentGrailsProp.isInherited()) {
            continue;
        }
        if (currentGrailsProp.isInherited() && isBidirectionalManyToOne && currentGrailsProp.isCircular()) {
            continue;
        }
        if (isCompositeIdProperty(gormMapping, currentGrailsProp))
            continue;
        if (isIdentityProperty(gormMapping, currentGrailsProp))
            continue;

        if (LOG.isDebugEnabled()) {
            LOG.debug("[GrailsDomainBinder] Binding persistent property [" + currentGrailsProp.getName() + "]");
        }

        Value value = null;

        // see if it's a collection type
        CollectionType collectionType = CollectionType.collectionTypeForClass(currentGrailsProp.getType());

        Class<?> userType = getUserType(currentGrailsProp);

        if (collectionType != null) {
            String typeName = getTypeName(currentGrailsProp, getPropertyConfig(currentGrailsProp), gormMapping);
            if (typeName != null && "serializable".equals(typeName)) {
                value = new SimpleValue(mappings, table);
                bindSimpleValue(typeName, (SimpleValue) value, currentGrailsProp.isOptional(),
                        getColumnNameForPropertyAndPath(currentGrailsProp, EMPTY_PATH, null,
                                sessionFactoryBeanName),
                        mappings);

            } else {
                // create collection
                Collection collection = collectionType.create(currentGrailsProp, persistentClass, EMPTY_PATH,
                        mappings, sessionFactoryBeanName);
                mappings.addCollection(collection);
                value = collection;
            }

        } else if (currentGrailsProp.isEnum()) {
            value = new SimpleValue(mappings, table);
            bindEnumType(currentGrailsProp, (SimpleValue) value, EMPTY_PATH, sessionFactoryBeanName);
        }
        // work out what type of relationship it is and bind value
        else if (currentGrailsProp.isManyToOne()) {
            if (LOG.isDebugEnabled())
                LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName()
                        + "] as ManyToOne");

            value = new ManyToOne(mappings, table);
            bindManyToOne(currentGrailsProp, (ManyToOne) value, EMPTY_PATH, mappings, sessionFactoryBeanName);
        } else if (currentGrailsProp.isOneToOne() && userType == null) {
            if (LOG.isDebugEnabled())
                LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName()
                        + "] as OneToOne");

            if (currentGrailsProp.isHasOne() && !currentGrailsProp.isBidirectional()) {
                throw new MappingException("hasOne property [" + currentGrailsProp.getDomainClass().getName()
                        + "." + currentGrailsProp.getName()
                        + "] is not bidirectional. Specify the other side of the relationship!");
            } else if (canBindOneToOneWithSingleColumnAndForeignKey(currentGrailsProp)) {
                value = new OneToOne(mappings, table, persistentClass);
                bindOneToOne(currentGrailsProp, (OneToOne) value, EMPTY_PATH, sessionFactoryBeanName);
            } else {
                if (currentGrailsProp.isHasOne() && currentGrailsProp.isBidirectional()) {
                    value = new OneToOne(mappings, table, persistentClass);
                    bindOneToOne(currentGrailsProp, (OneToOne) value, EMPTY_PATH, sessionFactoryBeanName);
                } else {
                    value = new ManyToOne(mappings, table);
                    bindManyToOne(currentGrailsProp, (ManyToOne) value, EMPTY_PATH, mappings,
                            sessionFactoryBeanName);
                }
            }
        } else if (currentGrailsProp.isEmbedded()) {
            value = new Component(mappings, persistentClass);

            bindComponent((Component) value, currentGrailsProp, true, mappings, sessionFactoryBeanName);
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName()
                        + "] as SimpleValue");
            }
            value = new SimpleValue(mappings, table);
            bindSimpleValue(currentGrailsProp, null, (SimpleValue) value, EMPTY_PATH, mappings,
                    sessionFactoryBeanName);
        }

        if (value != null) {
            Property property = createProperty(value, persistentClass, currentGrailsProp, mappings);
            persistentClass.addProperty(property);
        }
    }

    bindNaturalIdentifier(table, gormMapping, persistentClass);
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder.java

License:Apache License

private static void bindComponentProperty(Component component, GrailsDomainClassProperty componentProperty,
        GrailsDomainClassProperty currentGrailsProp, PersistentClass persistentClass, String path, Table table,
        Mappings mappings, String sessionFactoryBeanName) {
    Value value;/*from   w  ww . j  ava  2 s .co m*/
    // see if it's a collection type
    CollectionType collectionType = CollectionType.collectionTypeForClass(currentGrailsProp.getType());
    if (collectionType != null) {
        // create collection

        Collection collection = collectionType.create(currentGrailsProp, persistentClass, path, mappings,
                sessionFactoryBeanName);
        mappings.addCollection(collection);
        value = collection;
    }
    // work out what type of relationship it is and bind value
    else if (currentGrailsProp.isManyToOne()) {
        if (LOG.isDebugEnabled())
            LOG.debug(
                    "[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName() + "] as ManyToOne");

        value = new ManyToOne(mappings, table);
        bindManyToOne(currentGrailsProp, (ManyToOne) value, path, mappings, sessionFactoryBeanName);
    } else if (currentGrailsProp.isOneToOne()) {
        if (LOG.isDebugEnabled())
            LOG.debug(
                    "[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName() + "] as OneToOne");

        if (canBindOneToOneWithSingleColumnAndForeignKey(currentGrailsProp)) {
            value = new OneToOne(mappings, table, persistentClass);
            bindOneToOne(currentGrailsProp, (OneToOne) value, path, sessionFactoryBeanName);
        } else {
            value = new ManyToOne(mappings, table);
            bindManyToOne(currentGrailsProp, (ManyToOne) value, path, mappings, sessionFactoryBeanName);
        }
    } else if (currentGrailsProp.isEmbedded()) {
        value = new Component(mappings, persistentClass);
        bindComponent((Component) value, currentGrailsProp, true, mappings, sessionFactoryBeanName);
    } else {
        if (LOG.isDebugEnabled())
            LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName()
                    + "] as SimpleValue");

        value = new SimpleValue(mappings, table);
        if (currentGrailsProp.isEnum()) {
            bindEnumType(currentGrailsProp, (SimpleValue) value, path, sessionFactoryBeanName);
        } else {
            bindSimpleValue(currentGrailsProp, componentProperty, (SimpleValue) value, path, mappings,
                    sessionFactoryBeanName);
        }
    }

    if (value != null) {
        Property persistentProperty = createProperty(value, persistentClass, currentGrailsProp, mappings);
        component.addProperty(persistentProperty);
        if (isComponentPropertyNullable(componentProperty)) {
            final Iterator<?> columnIterator = value.getColumnIterator();
            while (columnIterator.hasNext()) {
                Column c = (Column) columnIterator.next();
                c.setNullable(true);
            }
        }
    }
}

From source file:org.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java

License:Apache License

/**
 * Creates and binds the properties for the specified Grails domain class and PersistentClass
 * and binds them to the Hibernate runtime meta model
 *
 * @param domainClass     The Grails domain class
 * @param persistentClass The Hibernate PersistentClass instance
 * @param mappings        The Hibernate Mappings instance
 * @param sessionFactoryBeanName  the session factory bean name
 */// w  w  w.  j  a v  a  2  s.  co m
protected void createClassProperties(HibernatePersistentEntity domainClass, PersistentClass persistentClass,
        Mappings mappings, String sessionFactoryBeanName) {

    final List<PersistentProperty> persistentProperties = domainClass.getPersistentProperties();
    Table table = persistentClass.getTable();

    Mapping gormMapping = domainClass.getMapping().getMappedForm();

    if (gormMapping != null) {
        table.setComment(gormMapping.getComment());
    }

    List<Embedded> embedded = new ArrayList<>();
    for (PersistentProperty currentGrailsProp : persistentProperties) {

        // if its inherited skip
        if (currentGrailsProp.isInherited()) {
            continue;
        }
        if (currentGrailsProp.getName().equals(GormProperties.VERSION))
            continue;
        if (isCompositeIdProperty(gormMapping, currentGrailsProp))
            continue;
        if (isIdentityProperty(gormMapping, currentGrailsProp))
            continue;

        if (LOG.isDebugEnabled()) {
            LOG.debug("[GrailsDomainBinder] Binding persistent property [" + currentGrailsProp.getName() + "]");
        }

        Value value = null;

        // see if it's a collection type
        CollectionType collectionType = CT.collectionTypeForClass(currentGrailsProp.getType());

        Class<?> userType = getUserType(currentGrailsProp);

        if (userType != null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName()
                        + "] as SimpleValue");
            }
            value = new SimpleValue(mappings, table);
            bindSimpleValue(currentGrailsProp, null, (SimpleValue) value, EMPTY_PATH, mappings,
                    sessionFactoryBeanName);
        } else if (collectionType != null) {
            String typeName = getTypeName(currentGrailsProp, getPropertyConfig(currentGrailsProp), gormMapping);
            if ("serializable".equals(typeName)) {
                value = new SimpleValue(mappings, table);
                bindSimpleValue(typeName, (SimpleValue) value, currentGrailsProp.isNullable(),
                        getColumnNameForPropertyAndPath(currentGrailsProp, EMPTY_PATH, null,
                                sessionFactoryBeanName),
                        mappings);
            } else {
                // create collection
                Collection collection = collectionType.create((ToMany) currentGrailsProp, persistentClass,
                        EMPTY_PATH, mappings, sessionFactoryBeanName);
                mappings.addCollection(collection);
                value = collection;
            }
        } else if (currentGrailsProp.getType().isEnum()) {
            value = new SimpleValue(mappings, table);
            bindEnumType(currentGrailsProp, (SimpleValue) value, EMPTY_PATH, sessionFactoryBeanName);
        } else if (currentGrailsProp instanceof Association) {
            Association association = (Association) currentGrailsProp;
            if (currentGrailsProp instanceof org.grails.datastore.mapping.model.types.ManyToOne) {
                if (LOG.isDebugEnabled())
                    LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName()
                            + "] as ManyToOne");

                value = new ManyToOne(mappings, table);
                bindManyToOne((Association) currentGrailsProp, (ManyToOne) value, EMPTY_PATH, mappings,
                        sessionFactoryBeanName);
            } else if (currentGrailsProp instanceof org.grails.datastore.mapping.model.types.OneToOne
                    && userType == null) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName()
                            + "] as OneToOne");
                }

                final boolean isHasOne = isHasOne(association);
                if (isHasOne && !association.isBidirectional()) {
                    throw new MappingException("hasOne property [" + currentGrailsProp.getOwner().getName()
                            + "." + currentGrailsProp.getName()
                            + "] is not bidirectional. Specify the other side of the relationship!");
                } else if (canBindOneToOneWithSingleColumnAndForeignKey((Association) currentGrailsProp)) {
                    value = new OneToOne(mappings, table, persistentClass);
                    bindOneToOne((org.grails.datastore.mapping.model.types.OneToOne) currentGrailsProp,
                            (OneToOne) value, EMPTY_PATH, sessionFactoryBeanName);
                } else {
                    if (isHasOne && association.isBidirectional()) {
                        value = new OneToOne(mappings, table, persistentClass);
                        bindOneToOne((org.grails.datastore.mapping.model.types.OneToOne) currentGrailsProp,
                                (OneToOne) value, EMPTY_PATH, sessionFactoryBeanName);
                    } else {
                        value = new ManyToOne(mappings, table);
                        bindManyToOne((Association) currentGrailsProp, (ManyToOne) value, EMPTY_PATH, mappings,
                                sessionFactoryBeanName);
                    }
                }
            } else if (currentGrailsProp instanceof Embedded) {
                embedded.add((Embedded) currentGrailsProp);
                continue;
            }
        }
        // work out what type of relationship it is and bind value
        else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName()
                        + "] as SimpleValue");
            }
            value = new SimpleValue(mappings, table);
            bindSimpleValue(currentGrailsProp, null, (SimpleValue) value, EMPTY_PATH, mappings,
                    sessionFactoryBeanName);
        }

        if (value != null) {
            Property property = createProperty(value, persistentClass, currentGrailsProp, mappings);
            persistentClass.addProperty(property);
        }
    }

    for (Embedded association : embedded) {
        Value value = new Component(mappings, persistentClass);

        bindComponent((Component) value, association, true, mappings, sessionFactoryBeanName);
        Property property = createProperty(value, persistentClass, association, mappings);
        persistentClass.addProperty(property);
    }

    bindNaturalIdentifier(table, gormMapping, persistentClass);
}

From source file:org.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java

License:Apache License

protected void bindComponentProperty(Component component, PersistentProperty componentProperty,
        PersistentProperty currentGrailsProp, PersistentClass persistentClass, String path, Table table,
        Mappings mappings, String sessionFactoryBeanName) {
    Value value;// w  ww  . jav a2s.  c  om
    // see if it's a collection type
    CollectionType collectionType = CT.collectionTypeForClass(currentGrailsProp.getType());
    if (collectionType != null) {
        // create collection
        Collection collection = collectionType.create((ToMany) currentGrailsProp, persistentClass, path,
                mappings, sessionFactoryBeanName);
        mappings.addCollection(collection);
        value = collection;
    }
    // work out what type of relationship it is and bind value
    else if (currentGrailsProp instanceof org.grails.datastore.mapping.model.types.ManyToOne) {
        if (LOG.isDebugEnabled())
            LOG.debug(
                    "[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName() + "] as ManyToOne");

        value = new ManyToOne(mappings, table);
        bindManyToOne((Association) currentGrailsProp, (ManyToOne) value, path, mappings,
                sessionFactoryBeanName);
    } else if (currentGrailsProp instanceof org.grails.datastore.mapping.model.types.OneToOne) {
        if (LOG.isDebugEnabled())
            LOG.debug(
                    "[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName() + "] as OneToOne");

        if (canBindOneToOneWithSingleColumnAndForeignKey((Association) currentGrailsProp)) {
            value = new OneToOne(mappings, table, persistentClass);
            bindOneToOne((org.grails.datastore.mapping.model.types.OneToOne) currentGrailsProp,
                    (OneToOne) value, path, sessionFactoryBeanName);
        } else {
            value = new ManyToOne(mappings, table);
            bindManyToOne((Association) currentGrailsProp, (ManyToOne) value, path, mappings,
                    sessionFactoryBeanName);
        }
    } else if (currentGrailsProp instanceof Embedded) {
        value = new Component(mappings, persistentClass);
        bindComponent((Component) value, (Embedded) currentGrailsProp, true, mappings, sessionFactoryBeanName);
    } else {
        if (LOG.isDebugEnabled())
            LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName()
                    + "] as SimpleValue");

        value = new SimpleValue(mappings, table);
        if (currentGrailsProp.getType().isEnum()) {
            bindEnumType(currentGrailsProp, (SimpleValue) value, path, sessionFactoryBeanName);
        } else {
            bindSimpleValue(currentGrailsProp, componentProperty, (SimpleValue) value, path, mappings,
                    sessionFactoryBeanName);
        }
    }

    if (value != null) {
        Property persistentProperty = createProperty(value, persistentClass, currentGrailsProp, mappings);
        component.addProperty(persistentProperty);
        if (isComponentPropertyNullable(componentProperty)) {
            final Iterator<?> columnIterator = value.getColumnIterator();
            while (columnIterator.hasNext()) {
                Column c = (Column) columnIterator.next();
                c.setNullable(true);
            }
        }
    }
}

From source file:org.grails.orm.hibernate.cfg.GrailsDomainBinder.java

License:Apache License

/**
 * Creates and binds the properties for the specified Grails domain class and PersistentClass
 * and binds them to the Hibernate runtime meta model
 *
 * @param domainClass     The Grails domain class
 * @param persistentClass The Hibernate PersistentClass instance
 * @param mappings        The Hibernate Mappings instance
 * @param sessionFactoryBeanName  the session factory bean name
 *///from www .  j a  v a  2s  .  co  m
protected void createClassProperties(HibernatePersistentEntity domainClass, PersistentClass persistentClass,
        InFlightMetadataCollector mappings, String sessionFactoryBeanName) {

    final List<PersistentProperty> persistentProperties = domainClass.getPersistentProperties();
    Table table = persistentClass.getTable();

    Mapping gormMapping = domainClass.getMapping().getMappedForm();

    if (gormMapping != null) {
        table.setComment(gormMapping.getComment());
    }

    List<Embedded> embedded = new ArrayList<>();

    for (PersistentProperty currentGrailsProp : persistentProperties) {

        // if its inherited skip
        if (currentGrailsProp.isInherited()) {
            continue;
        }
        if (currentGrailsProp.getName().equals(GormProperties.VERSION))
            continue;
        if (isCompositeIdProperty(gormMapping, currentGrailsProp))
            continue;
        if (isIdentityProperty(gormMapping, currentGrailsProp))
            continue;

        if (LOG.isDebugEnabled()) {
            LOG.debug("[GrailsDomainBinder] Binding persistent property [" + currentGrailsProp.getName() + "]");
        }

        Value value = null;

        // see if it's a collection type
        CollectionType collectionType = CT.collectionTypeForClass(currentGrailsProp.getType());

        Class<?> userType = getUserType(currentGrailsProp);

        if (userType != null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName()
                        + "] as SimpleValue");
            }
            value = new SimpleValue(mappings, table);
            bindSimpleValue(currentGrailsProp, null, (SimpleValue) value, EMPTY_PATH, mappings,
                    sessionFactoryBeanName);
        } else if (collectionType != null) {
            String typeName = getTypeName(currentGrailsProp, getPropertyConfig(currentGrailsProp), gormMapping);
            if ("serializable".equals(typeName)) {
                value = new SimpleValue(mappings, table);
                bindSimpleValue(typeName, (SimpleValue) value, currentGrailsProp.isNullable(),
                        getColumnNameForPropertyAndPath(currentGrailsProp, EMPTY_PATH, null,
                                sessionFactoryBeanName),
                        mappings);
            } else {
                // create collection
                Collection collection = collectionType.create((ToMany) currentGrailsProp, persistentClass,
                        EMPTY_PATH, mappings, sessionFactoryBeanName);
                mappings.addCollectionBinding(collection);
                value = collection;
            }
        } else if (currentGrailsProp.getType().isEnum()) {
            value = new SimpleValue(mappings, table);
            bindEnumType(currentGrailsProp, (SimpleValue) value, EMPTY_PATH, sessionFactoryBeanName);
        } else if (currentGrailsProp instanceof Association) {
            Association association = (Association) currentGrailsProp;
            if (currentGrailsProp instanceof org.grails.datastore.mapping.model.types.ManyToOne) {
                if (LOG.isDebugEnabled())
                    LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName()
                            + "] as ManyToOne");

                value = new ManyToOne(mappings, table);
                bindManyToOne((Association) currentGrailsProp, (ManyToOne) value, EMPTY_PATH, mappings,
                        sessionFactoryBeanName);
            } else if (currentGrailsProp instanceof org.grails.datastore.mapping.model.types.OneToOne
                    && userType == null) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName()
                            + "] as OneToOne");
                }

                final boolean isHasOne = isHasOne(association);
                if (isHasOne && !association.isBidirectional()) {
                    throw new MappingException("hasOne property [" + currentGrailsProp.getOwner().getName()
                            + "." + currentGrailsProp.getName()
                            + "] is not bidirectional. Specify the other side of the relationship!");
                } else if (canBindOneToOneWithSingleColumnAndForeignKey((Association) currentGrailsProp)) {
                    value = new OneToOne(mappings, table, persistentClass);
                    bindOneToOne((org.grails.datastore.mapping.model.types.OneToOne) currentGrailsProp,
                            (OneToOne) value, EMPTY_PATH, sessionFactoryBeanName);
                } else {
                    if (isHasOne && association.isBidirectional()) {
                        value = new OneToOne(mappings, table, persistentClass);
                        bindOneToOne((org.grails.datastore.mapping.model.types.OneToOne) currentGrailsProp,
                                (OneToOne) value, EMPTY_PATH, sessionFactoryBeanName);
                    } else {
                        value = new ManyToOne(mappings, table);
                        bindManyToOne((Association) currentGrailsProp, (ManyToOne) value, EMPTY_PATH, mappings,
                                sessionFactoryBeanName);
                    }
                }
            } else if (currentGrailsProp instanceof Embedded) {
                embedded.add((Embedded) currentGrailsProp);
                continue;
            }
        }
        // work out what type of relationship it is and bind value
        else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName()
                        + "] as SimpleValue");
            }
            value = new SimpleValue(mappings, table);
            bindSimpleValue(currentGrailsProp, null, (SimpleValue) value, EMPTY_PATH, mappings,
                    sessionFactoryBeanName);
        }

        if (value != null) {
            Property property = createProperty(value, persistentClass, currentGrailsProp, mappings);
            persistentClass.addProperty(property);
        }
    }

    for (Embedded association : embedded) {
        Value value = new Component(mappings, persistentClass);

        bindComponent((Component) value, association, true, mappings, sessionFactoryBeanName);
        Property property = createProperty(value, persistentClass, association, mappings);
        persistentClass.addProperty(property);
    }
    bindNaturalIdentifier(table, gormMapping, persistentClass);
}

From source file:org.grails.orm.hibernate.cfg.GrailsDomainBinder.java

License:Apache License

protected void bindComponentProperty(Component component, PersistentProperty componentProperty,
        PersistentProperty currentGrailsProp, PersistentClass persistentClass, String path, Table table,
        InFlightMetadataCollector mappings, String sessionFactoryBeanName) {
    Value value;//w  w w.  ja  v  a 2  s  .c  om
    // see if it's a collection type
    CollectionType collectionType = CT.collectionTypeForClass(currentGrailsProp.getType());
    if (collectionType != null) {
        // create collection
        Collection collection = collectionType.create((ToMany) currentGrailsProp, persistentClass, path,
                mappings, sessionFactoryBeanName);
        mappings.addCollectionBinding(collection);
        value = collection;
    }
    // work out what type of relationship it is and bind value
    else if (currentGrailsProp instanceof org.grails.datastore.mapping.model.types.ManyToOne) {
        if (LOG.isDebugEnabled())
            LOG.debug(
                    "[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName() + "] as ManyToOne");

        value = new ManyToOne(mappings, table);
        bindManyToOne((Association) currentGrailsProp, (ManyToOne) value, path, mappings,
                sessionFactoryBeanName);
    } else if (currentGrailsProp instanceof org.grails.datastore.mapping.model.types.OneToOne) {
        if (LOG.isDebugEnabled())
            LOG.debug(
                    "[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName() + "] as OneToOne");

        if (canBindOneToOneWithSingleColumnAndForeignKey((Association) currentGrailsProp)) {
            value = new OneToOne(mappings, table, persistentClass);
            bindOneToOne((org.grails.datastore.mapping.model.types.OneToOne) currentGrailsProp,
                    (OneToOne) value, path, sessionFactoryBeanName);
        } else {
            value = new ManyToOne(mappings, table);
            bindManyToOne((Association) currentGrailsProp, (ManyToOne) value, path, mappings,
                    sessionFactoryBeanName);
        }
    } else if (currentGrailsProp instanceof Embedded) {
        value = new Component(mappings, persistentClass);
        bindComponent((Component) value, (Embedded) currentGrailsProp, true, mappings, sessionFactoryBeanName);
    } else {
        if (LOG.isDebugEnabled())
            LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName()
                    + "] as SimpleValue");

        value = new SimpleValue(mappings, table);
        if (currentGrailsProp.getType().isEnum()) {
            bindEnumType(currentGrailsProp, (SimpleValue) value, path, sessionFactoryBeanName);
        } else {
            bindSimpleValue(currentGrailsProp, componentProperty, (SimpleValue) value, path, mappings,
                    sessionFactoryBeanName);
        }
    }

    if (value != null) {
        Property persistentProperty = createProperty(value, persistentClass, currentGrailsProp, mappings);
        component.addProperty(persistentProperty);
        if (isComponentPropertyNullable(componentProperty)) {
            final Iterator<?> columnIterator = value.getColumnIterator();
            while (columnIterator.hasNext()) {
                Column c = (Column) columnIterator.next();
                c.setNullable(true);
            }
        }
    }
}