List of usage examples for org.hibernate.boot.spi InFlightMetadataCollector addCollectionBinding
void addCollectionBinding(Collection collection) throws DuplicateMappingException;
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 *///w ww. j av a2 s . com 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;//from ww w.j av a 2s . c o m // 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); } } } }