List of usage examples for org.hibernate.mapping Collection getKey
public KeyValue getKey()
From source file:com.github.shyiko.rook.target.hibernate4.cache.PrimaryKey.java
License:Apache License
public PrimaryKey(Collection collection, Map<String, Integer> columnIndexByNameMap) { this(collection.getKey(), collection.getCollectionTable(), columnIndexByNameMap); if (positionWithinRow.length != 1) { throw new IllegalStateException("Unexpected PK length " + positionWithinRow.length); }//from w w w . j av a2 s.com }
From source file:com.tomitribe.reveng.codegen.FreemarkerObject.java
License:Apache License
public String generateManyToOneAnnotation(final BasicPOJOClass pojo, final Property p, String table) { // @ForeignKey(name="FK_recipe_tree_node") final Value value = p.getValue(); final int span; final Iterator columnIterator; if (value != null && value instanceof Collection) { final Collection collection = (Collection) value; span = collection.getKey().getColumnSpan(); columnIterator = collection.getKey().getColumnIterator(); } else {/* w ww. j av a 2s. c o m*/ span = p.getColumnSpan(); columnIterator = p.getColumnIterator(); } final Selectable selectable = (Selectable) columnIterator.next(); final Column column = (Column) selectable; table += "_" + column.getName(); // id_tree_node table = "FK_" + table.replace("_id_", "_"); String s = EntityPOJOClass.class.cast(pojo).generateManyToOneAnnotation(p); s += "\n@ForeignKey(name=\"" + table + "\")"; // log.info(s); return s; }
From source file:com.xpn.xwiki.store.migration.hibernate.R40000XWIKI6990DataMigration.java
License:Open Source License
/** * get name of the first column of the key of a given collection property. * @param coll the collection property/* w w w . jav a2 s . co m*/ * @return the column name of the key */ private String getKeyColumnName(org.hibernate.mapping.Collection coll) { return ((Column) coll.getKey().getColumnIterator().next()).getName(); }
From source file:edu.wustl.common.util.dbManager.HibernateMetaData.java
License:BSD License
/** * This function gets the key Id/* ww w. ja va 2 s . c o m*/ * from hibernate mappings and returns the value * @param attributeName * @return key Id * */ public static String getKeyId(String attributeName) { org.hibernate.mapping.Collection col1 = cfg.getCollectionMapping(attributeName); Iterator keyIt = col1.getKey().getColumnIterator(); while (keyIt.hasNext()) { Column col = (Column) keyIt.next(); return (col.getName()); } return ""; }
From source file:org.beangle.orm.hibernate.tool.DdlGenerator.java
License:Open Source License
/** * Generate sql scripts//from w ww. j av a 2s . co m * * @param fileName * @param packageName * @throws Exception */ @SuppressWarnings("unchecked") public void gen(String fileName, String packageName) throws Exception { configuration = ConfigBuilder.build(new OverrideConfiguration()); mapping = configuration.buildMapping(); defaultCatalog = configuration.getProperties().getProperty(Environment.DEFAULT_CATALOG); defaultSchema = configuration.getProperties().getProperty(Environment.DEFAULT_SCHEMA); configuration.getProperties().put(Environment.DIALECT, dialect); // 1. first process class mapping Iterator<PersistentClass> iterpc = configuration.getClassMappings(); while (iterpc.hasNext()) { PersistentClass pc = iterpc.next(); Class<?> clazz = pc.getMappedClass(); if (isNotBlank(packageName) && !clazz.getPackage().getName().startsWith(packageName)) continue; // add comment to table and column pc.getTable().setComment(messages.get(clazz, clazz.getSimpleName())); commentProperty(clazz, pc.getTable(), pc.getIdentifierProperty()); commentProperties(clazz, pc.getTable(), pc.getPropertyIterator()); // generator sequence sql if (pc instanceof RootClass) { IdentifierGenerator ig = pc.getIdentifier().createIdentifierGenerator( configuration.getIdentifierGeneratorFactory(), dialect, defaultCatalog, defaultSchema, (RootClass) pc); if (ig instanceof PersistentIdentifierGenerator) { String[] lines = ((PersistentIdentifierGenerator) ig).sqlCreateStrings(dialect); sequences.addAll(Arrays.asList(lines)); } } // generater table sql generateTableSql(pc.getTable()); } // 2. process collection mapping Iterator<Collection> itercm = configuration.getCollectionMappings(); while (itercm.hasNext()) { Collection col = itercm.next(); if (isNotBlank(packageName) && !col.getRole().startsWith(packageName)) continue; // collection sequences if (col.isIdentified()) { IdentifierGenerator ig = ((IdentifierCollection) col).getIdentifier().createIdentifierGenerator( configuration.getIdentifierGeneratorFactory(), dialect, defaultCatalog, defaultSchema, null); if (ig instanceof PersistentIdentifierGenerator) { String[] lines = ((PersistentIdentifierGenerator) ig).sqlCreateStrings(dialect); sequences.addAll(Arrays.asList(lines)); } } // collection table if (!col.isOneToMany()) { Table table = col.getCollectionTable(); String owner = col.getTable().getComment(); Class<?> ownerClass = col.getOwner().getMappedClass(); // resolved nested compoent name in collection's role String colName = substringAfter(col.getRole(), col.getOwnerEntityName() + "."); if (colName.contains(".")) ownerClass = getPropertyType(col.getOwner(), substringBeforeLast(colName, ".")); table.setComment(owner + "-" + messages.get(ownerClass, substringAfterLast(col.getRole(), "."))); Column keyColumn = table.getColumn((Column) col.getKey().getColumnIterator().next()); if (null != keyColumn) keyColumn.setComment(owner + " ID"); if (col instanceof IndexedCollection) { IndexedCollection idxCol = (IndexedCollection) col; Value idx = idxCol.getIndex(); if (idx instanceof ToOne) commentToOne((ToOne) idx, (Column) idx.getColumnIterator().next()); } if (col.getElement() instanceof ManyToOne) { Column valueColumn = (Column) col.getElement().getColumnIterator().next(); commentToOne((ManyToOne) col.getElement(), valueColumn); } else if (col.getElement() instanceof Component) { Component cp = (Component) col.getElement(); commentProperties(cp.getComponentClass(), table, cp.getPropertyIterator()); } generateTableSql(col.getCollectionTable()); } } Set<String> commentSet = CollectUtils.newHashSet(comments); comments.clear(); comments.addAll(commentSet); // 3. export to files for (String key : files.keySet()) { List<List<String>> sqls = files.get(key); FileWriter writer = new FileWriter(fileName + "/" + key, false); writes(writer, sqls); writer.flush(); writer.close(); } }
From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java
License:Apache License
/** * Binds a unidirectional one-to-many creating a psuedo back reference property in the process. * * @param property// w w w. jav a 2 s . co m * @param mappings * @param collection */ protected void bindUnidirectionalOneToMany(GrailsDomainClassProperty property, Mappings mappings, Collection collection) { Value v = collection.getElement(); v.createForeignKey(); String entityName; if (v instanceof ManyToOne) { ManyToOne manyToOne = (ManyToOne) v; entityName = manyToOne.getReferencedEntityName(); } else { entityName = ((OneToMany) v).getReferencedEntityName(); } collection.setInverse(false); PersistentClass referenced = mappings.getClass(entityName); Backref prop = new Backref(); prop.setEntityName(property.getDomainClass().getFullName()); prop.setName(UNDERSCORE + addUnderscore(property.getDomainClass().getShortName(), property.getName()) + "Backref"); prop.setUpdateable(false); prop.setInsertable(true); prop.setCollectionRole(collection.getRole()); prop.setValue(collection.getKey()); prop.setOptional(true); referenced.addProperty(prop); }
From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder.java
License:Apache License
/** * Binds a unidirectional one-to-many creating a psuedo back reference property in the process. * * @param property/*from w ww . j a va2 s. co m*/ * @param mappings * @param collection */ private static void bindUnidirectionalOneToMany(GrailsDomainClassProperty property, Mappings mappings, Collection collection) { Value v = collection.getElement(); v.createForeignKey(); String entityName; if (v instanceof ManyToOne) { ManyToOne manyToOne = (ManyToOne) v; entityName = manyToOne.getReferencedEntityName(); } else { entityName = ((OneToMany) v).getReferencedEntityName(); } collection.setInverse(false); PersistentClass referenced = mappings.getClass(entityName); Backref prop = new Backref(); prop.setEntityName(property.getDomainClass().getFullName()); prop.setName(UNDERSCORE + addUnderscore(property.getDomainClass().getShortName(), property.getName()) + "Backref"); prop.setUpdateable(false); prop.setInsertable(true); prop.setCollectionRole(collection.getRole()); prop.setValue(collection.getKey()); prop.setOptional(true); referenced.addProperty(prop); }
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. * // w w w . j ava 2s .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.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java
License:Apache License
/** * Binds a unidirectional one-to-many creating a psuedo back reference property in the process. * * @param property/*ww w . j a v a2s .c om*/ * @param mappings * @param collection */ protected void bindUnidirectionalOneToMany(org.grails.datastore.mapping.model.types.OneToMany property, Mappings mappings, Collection collection) { Value v = collection.getElement(); v.createForeignKey(); String entityName; if (v instanceof ManyToOne) { ManyToOne manyToOne = (ManyToOne) v; entityName = manyToOne.getReferencedEntityName(); } else { entityName = ((OneToMany) v).getReferencedEntityName(); } collection.setInverse(false); PersistentClass referenced = mappings.getClass(entityName); Backref prop = new Backref(); PersistentEntity owner = property.getOwner(); prop.setEntityName(owner.getName()); prop.setName( UNDERSCORE + addUnderscore(owner.getJavaClass().getSimpleName(), property.getName()) + "Backref"); prop.setUpdateable(false); prop.setInsertable(true); prop.setCollectionRole(collection.getRole()); prop.setValue(collection.getKey()); prop.setOptional(true); referenced.addProperty(prop); }
From source file:org.grails.orm.hibernate.cfg.GrailsDomainBinder.java
License:Apache License
/** * Binds a unidirectional one-to-many creating a psuedo back reference property in the process. * * @param property//from w ww . ja va 2 s .c o m * @param mappings * @param collection */ protected void bindUnidirectionalOneToMany(org.grails.datastore.mapping.model.types.OneToMany property, InFlightMetadataCollector mappings, Collection collection) { Value v = collection.getElement(); v.createForeignKey(); String entityName; if (v instanceof ManyToOne) { ManyToOne manyToOne = (ManyToOne) v; entityName = manyToOne.getReferencedEntityName(); } else { entityName = ((OneToMany) v).getReferencedEntityName(); } collection.setInverse(false); PersistentClass referenced = mappings.getEntityBinding(entityName); Backref prop = new Backref(); PersistentEntity owner = property.getOwner(); prop.setEntityName(owner.getName()); prop.setName( UNDERSCORE + addUnderscore(owner.getJavaClass().getSimpleName(), property.getName()) + "Backref"); prop.setUpdateable(false); prop.setInsertable(true); prop.setCollectionRole(collection.getRole()); prop.setValue(collection.getKey()); prop.setOptional(true); referenced.addProperty(prop); }