List of usage examples for org.hibernate.mapping Property Property
Property
From source file:org.eclipse.emf.teneo.hibernate.HbDataStore.java
License:Open Source License
/** * Extra lazy mapping for lists needs a real property for the list index and * a real inverse for the other side as well. * /*from ww w . j av a 2 s . c om*/ * This method iterates over all associations and adds an inverse for the * list and set mappings. */ protected void addExtraLazyInverseProperties() { final Map<String, PersistentClass> persistentClasses = new HashMap<String, PersistentClass>(); for (Iterator<?> pcs = getClassMappings(); pcs.hasNext();) { final PersistentClass pc = (PersistentClass) pcs.next(); if (isClassOrSuperClassEAVMapped(pc)) { continue; } persistentClasses.put(pc.getEntityName(), pc); } for (Iterator<?> pcs = getClassMappings(); pcs.hasNext();) { final PersistentClass pc = (PersistentClass) pcs.next(); // copy to prevent concurrent modification final Iterator<?> propIt = pc.getPropertyIterator(); final List<Property> props = new ArrayList<Property>(); while (propIt.hasNext()) { final Property prop = (Property) propIt.next(); props.add(prop); } for (Property prop : props) { EClass eClass = null; if (pc.getMetaAttribute(HbMapperConstants.FEATUREMAP_META) == null) { if (pc.getEntityName() != null) { eClass = getEntityNameStrategy().toEClass(pc.getEntityName()); } else { eClass = EModelResolver.instance().getEClass(pc.getMappedClass()); } } final EStructuralFeature ef = eClass == null ? null : StoreUtil.getEStructuralFeature(eClass, prop.getName()); if (ef != null && ef instanceof EReference && prop.getValue() instanceof Collection) { final Collection collection = (Collection) prop.getValue(); final EReference eReference = (EReference) ef; // only work for extra lazy if (!collection.isExtraLazy()) { continue; } final Value elementValue = collection.getElement(); final PersistentClass elementPC; if (elementValue instanceof OneToMany) { final OneToMany oneToMany = (OneToMany) elementValue; elementPC = oneToMany.getAssociatedClass(); } else if (elementValue instanceof ManyToOne) { final ManyToOne mto = (ManyToOne) elementValue; elementPC = persistentClasses.get(mto.getReferencedEntityName()); } else { continue; } if (isClassOrSuperClassEAVMapped(elementPC)) { continue; } collection.setInverse(true); // and add an eopposite if (eReference.getEOpposite() == null) { final Table collectionTable = collection.getCollectionTable(); if (isClassOrSuperClassEAVMapped(elementPC)) { continue; } final Property inverseRefProperty = new Property(); inverseRefProperty.setName(StoreUtil.getExtraLazyInversePropertyName(ef)); final Map<Object, Object> metas = new HashMap<Object, Object>(); final MetaAttribute metaAttribute = new MetaAttribute( HbConstants.SYNTHETIC_PROPERTY_INDICATOR); metaAttribute.addValue("true"); metas.put(HbConstants.SYNTHETIC_PROPERTY_INDICATOR, metaAttribute); inverseRefProperty.setMetaAttributes(metas); inverseRefProperty.setNodeName(inverseRefProperty.getName()); inverseRefProperty.setPropertyAccessorName(SyntheticPropertyHandler.class.getName()); inverseRefProperty.setLazy(false); final ManyToOne mto = new ManyToOne(getMappings(), collectionTable); mto.setReferencedEntityName(pc.getEntityName()); mto.setLazy(false); mto.setFetchMode(FetchMode.SELECT); inverseRefProperty.setValue(mto); final Iterator<?> it = collection.getKey().getColumnIterator(); while (it.hasNext()) { final Column originalColumn = (Column) it.next(); // final Column newColumn = new // Column(originalColumn.getName()); mto.addColumn(originalColumn); } mto.createForeignKey(); // now determine if a join should be created if (collectionTable.getName().equalsIgnoreCase(elementPC.getTable().getName())) { elementPC.addProperty(inverseRefProperty); } else { // create a join final Join join = new Join(); join.setPersistentClass(elementPC); join.setTable(collectionTable); join.addProperty(inverseRefProperty); final ManyToOne keyValue = new ManyToOne(getMappings(), collectionTable); join.setKey(keyValue); @SuppressWarnings("unchecked") final Iterator<Column> keyColumns = collection.getElement().getColumnIterator(); while (keyColumns.hasNext()) { keyValue.addColumn(keyColumns.next()); } keyValue.setReferencedEntityName(elementPC.getEntityName()); keyValue.setTable(collectionTable); keyValue.createForeignKey(); elementPC.addJoin(join); } } // add an opposite index if (collection.isIndexed() && !collection.isMap()) { Table collectionTable = collection.getCollectionTable(); IndexedCollection indexedCollection = (IndexedCollection) collection; final Column column = (Column) indexedCollection.getIndex().getColumnIterator().next(); final Property indexProperty = new Property(); indexProperty.setName(StoreUtil.getExtraLazyInverseIndexPropertyName(ef)); final Map<Object, Object> metas = new HashMap<Object, Object>(); final MetaAttribute metaAttribute = new MetaAttribute( HbConstants.SYNTHETIC_PROPERTY_INDICATOR); metaAttribute.addValue("true"); metas.put(HbConstants.SYNTHETIC_PROPERTY_INDICATOR, metaAttribute); indexProperty.setMetaAttributes(metas); indexProperty.setNodeName(indexProperty.getName()); indexProperty.setPropertyAccessorName(SyntheticPropertyHandler.class.getName()); // always make this nullable, nullability is controlled // by the main property indexProperty.setOptional(true); Join join = null; @SuppressWarnings("unchecked") final Iterator<Join> it = (Iterator<Join>) elementPC.getJoinIterator(); while (it.hasNext()) { final Join foundJoin = it.next(); if (foundJoin.getTable().getName().equalsIgnoreCase(collectionTable.getName())) { join = foundJoin; collectionTable = join.getTable(); break; } } final SimpleValue sv = new SimpleValue(getMappings(), indexedCollection.getIndex().getTable()); sv.setTypeName("integer"); // final Column svColumn = new Column(column.getName()); sv.addColumn(column); // checkColumnExists(collectionTable, // svColumn)); indexProperty.setValue(sv); if (join != null) { join.addProperty(indexProperty); } else { elementPC.addProperty(indexProperty); } } } } } }
From source file:org.eclipse.emf.teneo.hibernate.HbDataStore.java
License:Open Source License
protected void addContainerMappingToPC(PersistentClass pc) { if (log.isDebugEnabled()) { log.debug("Adding eContainer and econtainerfeatureid properties to " + pc.getClassName()); }/*w w w . j ava2 s . co m*/ final EContainerFeaturePersistenceStrategy featurePersistenceStrategy = getPersistenceOptions() .getEContainerFeaturePersistenceStrategy(); final Property eContainer = new Property(); eContainer.setName(HbConstants.PROPERTY_ECONTAINER); eContainer.setMetaAttributes(new HashMap<Object, Object>()); eContainer.setNodeName(eContainer.getName()); eContainer.setPropertyAccessorName(EContainerAccessor.class.getName()); final SimpleValue sv = new SimpleValue(getMappings(), pc.getTable()); sv.setTypeName(EContainerUserType.class.getName()); final Column eccColumn = new Column(getPersistenceOptions().getSQLColumnNamePrefix() + getPersistenceOptions().getEContainerClassColumn()); sv.addColumn(checkColumnExists(pc.getTable(), eccColumn)); final Column ecColumn = new Column( getPersistenceOptions().getSQLColumnNamePrefix() + getPersistenceOptions().getEContainerColumn()); sv.addColumn(checkColumnExists(pc.getTable(), ecColumn)); eContainer.setValue(sv); pc.addProperty(eContainer); if (featurePersistenceStrategy.equals(EContainerFeaturePersistenceStrategy.FEATUREID) || featurePersistenceStrategy.equals(EContainerFeaturePersistenceStrategy.BOTH)) { final Property ecFID = new Property(); ecFID.setName(HbConstants.PROPERTY_ECONTAINER_FEATURE_ID); ecFID.setMetaAttributes(new HashMap<Object, Object>()); ecFID.setNodeName(ecFID.getName()); ecFID.setPropertyAccessorName(EContainerFeatureIDAccessor.class.getName()); final SimpleValue svfid = new SimpleValue(getMappings(), pc.getTable()); svfid.setTypeName("integer"); final Column ecfColumn = new Column( getPersistenceOptions().getSQLColumnNamePrefix() + HbConstants.COLUMN_ECONTAINER_FEATUREID); svfid.addColumn(checkColumnExists(pc.getTable(), ecfColumn)); ecFID.setValue(svfid); pc.addProperty(ecFID); } if (featurePersistenceStrategy.equals(EContainerFeaturePersistenceStrategy.FEATURENAME) || featurePersistenceStrategy.equals(EContainerFeaturePersistenceStrategy.BOTH)) { final Property ecFID = new Property(); ecFID.setName(HbConstants.PROPERTY_ECONTAINER_FEATURE_NAME); ecFID.setMetaAttributes(new HashMap<Object, Object>()); ecFID.setNodeName(ecFID.getName()); ecFID.setPropertyAccessorName(NewEContainerFeatureIDPropertyHandler.class.getName()); final SimpleValue svfid = new SimpleValue(getMappings(), pc.getTable()); svfid.setTypeName(EContainerFeatureIDUserType.class.getName()); final Column ecfColumn = new Column(getPersistenceOptions().getSQLColumnNamePrefix() + getPersistenceOptions().getEContainerFeatureNameColumn()); ecfColumn.setLength(getEContainerFeatureNameColumnLength()); svfid.addColumn(checkColumnExists(pc.getTable(), ecfColumn)); ecFID.setValue(svfid); pc.addProperty(ecFID); } }
From source file:org.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java
License:Apache License
protected Property createProperty(Value value, PersistentClass persistentClass, PersistentProperty grailsProperty, Mappings mappings) { // set type/*ww w. j av a2s.c om*/ value.setTypeUsingReflection(persistentClass.getClassName(), grailsProperty.getName()); if (value.getTable() != null) { value.createForeignKey(); } Property prop = new Property(); prop.setValue(value); bindProperty(grailsProperty, prop, mappings); return prop; }
From source file:org.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java
License:Apache License
protected void bindVersion(PersistentProperty version, RootClass entity, Mappings mappings, String sessionFactoryBeanName) { if (version != null) { SimpleValue val = new SimpleValue(mappings, entity.getTable()); bindSimpleValue(version, null, val, EMPTY_PATH, mappings, sessionFactoryBeanName); if (val.isTypeSpecified()) { if (!(val.getType() instanceof IntegerType || val.getType() instanceof LongType || val.getType() instanceof TimestampType)) { LOG.warn("Invalid version class specified in " + version.getOwner().getName() + "; must be one of [int, Integer, long, Long, Timestamp, Date]. Not mapping the version."); return; }//from w ww. j a v a2 s . c om } else { val.setTypeName("version".equals(version.getName()) ? "integer" : "timestamp"); } Property prop = new Property(); prop.setValue(val); bindProperty(version, prop, mappings); val.setNullValue("undefined"); entity.setVersion(prop); entity.setOptimisticLockMode(0); // 0 is to use version column entity.addProperty(prop); } }
From source file:org.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java
License:Apache License
@SuppressWarnings("unchecked") protected void bindSimpleId(PersistentProperty identifier, RootClass entity, Mappings mappings, Identity mappedId, String sessionFactoryBeanName) { Mapping mapping = getMapping(identifier.getOwner()); boolean useSequence = mapping != null && mapping.isTablePerConcreteClass(); // create the id value SimpleValue id = new SimpleValue(mappings, entity.getTable()); // set identifier on entity Properties params = new Properties(); entity.setIdentifier(id);// w ww . j a va 2s.c o m if (mappedId == null) { // configure generator strategy id.setIdentifierGeneratorStrategy(useSequence ? "sequence-identity" : "native"); } else { String generator = mappedId.getGenerator(); if ("native".equals(generator) && useSequence) { generator = "sequence-identity"; } id.setIdentifierGeneratorStrategy(generator); params.putAll(mappedId.getParams()); if ("assigned".equals(generator)) { id.setNullValue("undefined"); } } params.put(PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, mappings.getObjectNameNormalizer()); if (mappings.getSchemaName() != null) { params.setProperty(PersistentIdentifierGenerator.SCHEMA, mappings.getSchemaName()); } if (mappings.getCatalogName() != null) { params.setProperty(PersistentIdentifierGenerator.CATALOG, mappings.getCatalogName()); } id.setIdentifierGeneratorProperties(params); // bind value bindSimpleValue(identifier, null, id, EMPTY_PATH, mappings, sessionFactoryBeanName); // create property Property prop = new Property(); prop.setValue(id); // bind property bindProperty(identifier, prop, mappings); // set identifier property entity.setIdentifierProperty(prop); id.getTable().setIdentifierValue(id); }
From source file:org.grails.orm.hibernate.cfg.GrailsDomainBinder.java
License:Apache License
protected Property createProperty(Value value, PersistentClass persistentClass, PersistentProperty grailsProperty, InFlightMetadataCollector mappings) { // set type/*from w ww. j a v a 2 s. c om*/ value.setTypeUsingReflection(persistentClass.getClassName(), grailsProperty.getName()); if (value.getTable() != null) { value.createForeignKey(); } Property prop = new Property(); prop.setValue(value); bindProperty(grailsProperty, prop, mappings); return prop; }
From source file:org.grails.orm.hibernate.cfg.GrailsDomainBinder.java
License:Apache License
protected void bindVersion(PersistentProperty version, RootClass entity, InFlightMetadataCollector mappings, String sessionFactoryBeanName) { if (version != null) { SimpleValue val = new SimpleValue(mappings, entity.getTable()); bindSimpleValue(version, null, val, EMPTY_PATH, mappings, sessionFactoryBeanName); if (val.isTypeSpecified()) { if (!(val.getType() instanceof IntegerType || val.getType() instanceof LongType || val.getType() instanceof TimestampType)) { LOG.warn("Invalid version class specified in " + version.getOwner().getName() + "; must be one of [int, Integer, long, Long, Timestamp, Date]. Not mapping the version."); return; }//from w w w . ja v a 2 s . c o m } else { val.setTypeName("version".equals(version.getName()) ? "integer" : "timestamp"); } Property prop = new Property(); prop.setValue(val); bindProperty(version, prop, mappings); prop.setLazy(false); val.setNullValue("undefined"); entity.setVersion(prop); entity.setOptimisticLockStyle(OptimisticLockStyle.VERSION); entity.addProperty(prop); } }
From source file:org.grails.orm.hibernate.cfg.GrailsDomainBinder.java
License:Apache License
@SuppressWarnings("unchecked") protected void bindSimpleId(PersistentProperty identifier, RootClass entity, InFlightMetadataCollector mappings, Identity mappedId, String sessionFactoryBeanName) { Mapping mapping = getMapping(identifier.getOwner()); boolean useSequence = mapping != null && mapping.isTablePerConcreteClass(); // create the id value SimpleValue id = new SimpleValue(mappings, entity.getTable()); // set identifier on entity Properties params = new Properties(); entity.setIdentifier(id);/*from w w w . j a v a2s . c o m*/ if (mappedId == null) { // configure generator strategy id.setIdentifierGeneratorStrategy(useSequence ? "sequence-identity" : "native"); } else { String generator = mappedId.getGenerator(); if ("native".equals(generator) && useSequence) { generator = "sequence-identity"; } id.setIdentifierGeneratorStrategy(generator); params.putAll(mappedId.getParams()); if (params.containsKey(SEQUENCE_KEY)) { params.put(SequenceStyleGenerator.SEQUENCE_PARAM, params.getProperty(SEQUENCE_KEY)); } if ("assigned".equals(generator)) { id.setNullValue("undefined"); } } String schemaName = getSchemaName(mappings); String catalogName = getCatalogName(mappings); params.put(PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, this.metadataBuildingContext.getObjectNameNormalizer()); if (schemaName != null) { params.setProperty(PersistentIdentifierGenerator.SCHEMA, schemaName); } if (catalogName != null) { params.setProperty(PersistentIdentifierGenerator.CATALOG, catalogName); } id.setIdentifierGeneratorProperties(params); // bind value bindSimpleValue(identifier, null, id, EMPTY_PATH, mappings, sessionFactoryBeanName); // create property Property prop = new Property(); prop.setValue(id); // bind property bindProperty(identifier, prop, mappings); // set identifier property entity.setIdentifierProperty(prop); id.getTable().setIdentifierValue(id); }
From source file:org.infinispan.test.hibernate.cache.commons.functional.AbstractFunctionalTest.java
License:LGPL
@Override protected void afterMetadataBuilt(Metadata metadata) { if (addVersions) { for (PersistentClass clazz : metadata.getEntityBindings()) { if (clazz.getVersion() != null) { continue; }//from w w w.j a va2 s . co m try { clazz.getMappedClass().getMethod("getVersion"); clazz.getMappedClass().getMethod("setVersion", long.class); } catch (NoSuchMethodException e) { continue; } RootClass rootClazz = clazz.getRootClass(); Property versionProperty = new Property(); versionProperty.setName("version"); SimpleValue value = new SimpleValue((MetadataImplementor) metadata, rootClazz.getTable()); value.setTypeName("long"); Column column = new Column(); column.setValue(value); column.setName("version"); value.addColumn(column); rootClazz.getTable().addColumn(column); versionProperty.setValue(value); rootClazz.setVersion(versionProperty); rootClazz.addProperty(versionProperty); } } }
From source file:org.jboss.tools.hibernate.ui.diagram.editors.model.SpecialRootClass.java
License:Open Source License
@SuppressWarnings("unchecked") private void generate() { if (property == null) { return;//from ww w . ja v a 2 s .c o m } Component component = null; if (property.getValue() instanceof Collection) { Collection collection = (Collection) property.getValue(); component = (Component) collection.getElement(); } else if (property.getValue() instanceof Component) { component = (Component) property.getValue(); } if (component != null) { setClassName(component.getComponentClassName()); setEntityName(component.getComponentClassName()); PersistentClass ownerClass = component.getOwner(); if (component.getParentProperty() != null) { parentProperty = new Property(); parentProperty.setName(component.getParentProperty()); parentProperty.setPersistentClass(ownerClass); } Iterator<Property> iterator = component.getPropertyIterator(); while (iterator.hasNext()) { Property property = iterator.next(); if (property != null) { addProperty(property); } } } }