List of usage examples for org.hibernate.boot.spi InFlightMetadataCollector addEntityBinding
void addEntityBinding(PersistentClass persistentClass) throws DuplicateMappingException;
From source file:org.grails.orm.hibernate.cfg.GrailsDomainBinder.java
License:Apache License
/** * Binds a root class (one with no super classes) to the runtime meta model * based on the supplied Grails domain class * * @param entity The Grails domain class * @param mappings The Hibernate Mappings object * @param sessionFactoryBeanName the session factory bean name *//* ww w . j a v a2 s . c o m*/ public void bindRoot(HibernatePersistentEntity entity, InFlightMetadataCollector mappings, String sessionFactoryBeanName) { if (mappings.getEntityBinding(entity.getName()) != null) { LOG.info("[GrailsDomainBinder] Class [" + entity.getName() + "] is already mapped, skipping.. "); return; } RootClass root = new RootClass(this.metadataBuildingContext); root.setAbstract(entity.isAbstract()); final MappingContext mappingContext = entity.getMappingContext(); final java.util.Collection<PersistentEntity> children = mappingContext.getDirectChildEntities(entity); if (children.isEmpty()) { root.setPolymorphic(false); } bindClass(entity, root, mappings); Mapping m = getMapping(entity); bindRootPersistentClassCommonValues(entity, root, mappings, sessionFactoryBeanName); if (!children.isEmpty()) { boolean tablePerSubclass = m != null && !m.getTablePerHierarchy(); if (!tablePerSubclass) { // if the root class has children create a discriminator property bindDiscriminatorProperty(root.getTable(), root, mappings); } // bind the sub classes bindSubClasses(entity, root, mappings, sessionFactoryBeanName); } if (entity.isMultiTenant()) { TenantId tenantId = entity.getTenantId(); if (tenantId != null) { String filterCondition = getMultiTenantFilterCondition(sessionFactoryBeanName, entity); root.addFilter(GormProperties.TENANT_IDENTITY, filterCondition, true, Collections.<String, String>emptyMap(), Collections.<String, String>emptyMap()); mappings.addFilterDefinition(new FilterDefinition(GormProperties.TENANT_IDENTITY, filterCondition, Collections.singletonMap(GormProperties.TENANT_IDENTITY, root.getProperty(tenantId.getName()).getType()))); } } mappings.addEntityBinding(root); }
From source file:org.grails.orm.hibernate.cfg.GrailsDomainBinder.java
License:Apache License
/** * Binds a sub class.// www. j a v a 2 s .c om * * @param sub The sub domain class instance * @param parent The parent persistent class instance * @param mappings The mappings instance * @param sessionFactoryBeanName the session factory bean name */ protected void bindSubClass(HibernatePersistentEntity sub, PersistentClass parent, InFlightMetadataCollector mappings, String sessionFactoryBeanName) { evaluateMapping(sub, defaultMapping); Mapping m = getMapping(parent.getMappedClass()); Subclass subClass; boolean tablePerSubclass = m != null && !m.getTablePerHierarchy() && !m.isTablePerConcreteClass(); boolean tablePerConcreteClass = m != null && m.isTablePerConcreteClass(); final String fullName = sub.getName(); if (tablePerSubclass) { subClass = new JoinedSubclass(parent, this.metadataBuildingContext); } else if (tablePerConcreteClass) { subClass = new UnionSubclass(parent, this.metadataBuildingContext); } else { subClass = new SingleTableSubclass(parent, this.metadataBuildingContext); // set the descriminator value as the name of the class. This is the // value used by Hibernate to decide what the type of the class is // to perform polymorphic queries Mapping subMapping = getMapping(sub); DiscriminatorConfig discriminatorConfig = subMapping != null ? subMapping.getDiscriminator() : null; subClass.setDiscriminatorValue(discriminatorConfig != null && discriminatorConfig.getValue() != null ? discriminatorConfig.getValue() : fullName); if (subMapping != null) { configureDerivedProperties(sub, subMapping); } } subClass.setEntityName(fullName); subClass.setJpaEntityName(unqualify(fullName)); parent.addSubclass(subClass); mappings.addEntityBinding(subClass); if (tablePerSubclass) { bindJoinedSubClass(sub, (JoinedSubclass) subClass, mappings, m, sessionFactoryBeanName); } else if (tablePerConcreteClass) { bindUnionSubclass(sub, (UnionSubclass) subClass, mappings, sessionFactoryBeanName); } else { bindSubClass(sub, subClass, mappings, sessionFactoryBeanName); } final java.util.Collection<PersistentEntity> childEntities = sub.getMappingContext() .getDirectChildEntities(sub); if (!childEntities.isEmpty()) { // bind the sub classes bindSubClasses(sub, subClass, mappings, sessionFactoryBeanName); } }