List of usage examples for org.hibernate.mapping PersistentClass getIdentifier
public abstract KeyValue getIdentifier();
From source file:com.github.shyiko.rook.target.hibernate4.cache.PrimaryKey.java
License:Apache License
public PrimaryKey(PersistentClass persistentClass, Map<String, Integer> columnIndexByNameMap) { this(persistentClass.getKey(), persistentClass.getTable(), columnIndexByNameMap); final Type type = persistentClass.getIdentifier().getType(); if (type instanceof EmbeddedComponentType) { entityClass = type.getReturnedClass(); } else {/*from www. ja v a2s . c o m*/ entityClass = persistentClass.getMappedClass(); } }
From source file:com.manydesigns.portofino.persistence.hibernate.HibernateConfig.java
License:Open Source License
private Property getRefProperty(PersistentClass clazzOne, String propertyName) { Property refProp;/*w w w .j av a 2s . c o m*/ //TODO alessio ha senso questo automatismo? if (null != clazzOne.getIdentifierProperty()) { refProp = clazzOne.getIdentifierProperty(); } else if (null != clazzOne.getIdentifier()) { refProp = ((Component) clazzOne.getIdentifier()).getProperty(propertyName); } else { refProp = clazzOne.getProperty(propertyName); } return refProp; }
From source file:com.mysema.query.jpa.codegen.HibernateDomainExporter.java
License:Apache License
private void collectTypes() throws IOException, XMLStreamException, ClassNotFoundException, SecurityException, NoSuchMethodException {//ww w.ja v a 2 s .c o m // super classes Iterator<?> superClassMappings = configuration.getMappedSuperclassMappings(); while (superClassMappings.hasNext()) { MappedSuperclass msc = (MappedSuperclass) superClassMappings.next(); EntityType entityType = createSuperType(msc.getMappedClass()); if (msc.getDeclaredIdentifierProperty() != null) { handleProperty(entityType, msc.getMappedClass(), msc.getDeclaredIdentifierProperty()); } Iterator<?> properties = msc.getDeclaredPropertyIterator(); while (properties.hasNext()) { handleProperty(entityType, msc.getMappedClass(), (org.hibernate.mapping.Property) properties.next()); } } // entity classes Iterator<?> classMappings = configuration.getClassMappings(); while (classMappings.hasNext()) { PersistentClass pc = (PersistentClass) classMappings.next(); EntityType entityType = createEntityType(pc.getMappedClass()); if (pc.getDeclaredIdentifierProperty() != null) { handleProperty(entityType, pc.getMappedClass(), pc.getDeclaredIdentifierProperty()); } else if (!pc.isInherited() && pc.hasIdentifierProperty()) { logger.info(entityType.toString() + pc.getIdentifierProperty()); handleProperty(entityType, pc.getMappedClass(), pc.getIdentifierProperty()); } else if (pc.getIdentifier() != null) { KeyValue identifier = pc.getIdentifier(); if (identifier instanceof Component) { Component component = (Component) identifier; Iterator<?> properties = component.getPropertyIterator(); if (component.isEmbedded()) { while (properties.hasNext()) { handleProperty(entityType, pc.getMappedClass(), (org.hibernate.mapping.Property) properties.next()); } } else { String name = component.getNodeName(); Class<?> clazz = component.getType().getReturnedClass(); Type propertyType = getType(pc.getMappedClass(), clazz, name); AnnotatedElement annotated = getAnnotatedElement(pc.getMappedClass(), name); Property property = createProperty(entityType, name, propertyType, annotated); entityType.addProperty(property); // handle component properties EntityType embeddedType = createEmbeddableType(propertyType); while (properties.hasNext()) { handleProperty(embeddedType, clazz, (org.hibernate.mapping.Property) properties.next()); } } } // TODO handle other KeyValue subclasses such as Any, DependentValue and ToOne } Iterator<?> properties = pc.getDeclaredPropertyIterator(); while (properties.hasNext()) { handleProperty(entityType, pc.getMappedClass(), (org.hibernate.mapping.Property) properties.next()); } } // go through supertypes Set<Supertype> additions = Sets.newHashSet(); for (Map.Entry<String, EntityType> entry : allTypes.entrySet()) { EntityType entityType = entry.getValue(); if (entityType.getSuperType() != null && !allTypes.containsKey(entityType.getSuperType().getType().getFullName())) { additions.add(entityType.getSuperType()); } } for (Supertype type : additions) { type.setEntityType(createEntityType(type.getType(), superTypes)); } }
From source file:com.netspective.tool.hibernate.document.diagram.HibernateDiagramGenerator.java
License:Open Source License
public HibernateDiagramGenerator(final Configuration configuration, final GraphvizDiagramGenerator graphvizDiagramGenerator, final HibernateDiagramGeneratorFilter schemaDiagramFilter) throws HibernateDiagramGeneratorException { this.configuration = configuration; this.graphvizDiagramGenerator = graphvizDiagramGenerator; this.diagramFilter = schemaDiagramFilter; // the following was copied from org.hibernate.cfg.Configuration.buildMapping() because buildMapping() was private this.mapping = new Mapping() { /**//w w w . j a va 2s . c o m * Returns the identifier type of a mapped class */ public Type getIdentifierType(String persistentClass) throws MappingException { final PersistentClass pc = configuration.getClassMapping(persistentClass); if (pc == null) throw new MappingException("persistent class not known: " + persistentClass); return pc.getIdentifier().getType(); } public String getIdentifierPropertyName(String persistentClass) throws MappingException { final PersistentClass pc = configuration.getClassMapping(persistentClass); if (pc == null) throw new MappingException("persistent class not known: " + persistentClass); if (!pc.hasIdentifierProperty()) return null; return pc.getIdentifierProperty().getName(); } public Type getPropertyType(String persistentClass, String propertyName) throws MappingException { final PersistentClass pc = configuration.getClassMapping(persistentClass); if (pc == null) throw new MappingException("persistent class not known: " + persistentClass); Property prop = pc.getProperty(propertyName); if (prop == null) throw new MappingException("property not known: " + persistentClass + '.' + propertyName); return prop.getType(); } }; String dialectName = configuration.getProperty(Environment.DIALECT); if (dialectName == null) dialectName = org.hibernate.dialect.GenericDialect.class.getName(); try { final Class cls = Class.forName(dialectName); this.dialect = (Dialect) cls.newInstance(); } catch (Exception e) { throw new HibernateDiagramGeneratorException(e); } for (final Iterator classes = configuration.getClassMappings(); classes.hasNext();) { final PersistentClass pclass = (PersistentClass) classes.next(); final Table table = (Table) pclass.getTable(); tableToClassMap.put(table, pclass); } }
From source file:com.querydsl.jpa.codegen.HibernateDomainExporter.java
License:Apache License
@Override protected void collectTypes() throws IOException, XMLStreamException, ClassNotFoundException, NoSuchMethodException { // super classes Iterator<?> superClassMappings = configuration.getMappedSuperclassMappings(); while (superClassMappings.hasNext()) { MappedSuperclass msc = (MappedSuperclass) superClassMappings.next(); EntityType entityType = createSuperType(msc.getMappedClass()); if (msc.getDeclaredIdentifierProperty() != null) { handleProperty(entityType, msc.getMappedClass(), msc.getDeclaredIdentifierProperty()); }/* ww w .j ava2s . c o m*/ Iterator<?> properties = msc.getDeclaredPropertyIterator(); while (properties.hasNext()) { handleProperty(entityType, msc.getMappedClass(), (org.hibernate.mapping.Property) properties.next()); } } // entity classes Iterator<?> classMappings = configuration.getClassMappings(); while (classMappings.hasNext()) { PersistentClass pc = (PersistentClass) classMappings.next(); EntityType entityType = createEntityType(pc.getMappedClass()); if (pc.getDeclaredIdentifierProperty() != null) { handleProperty(entityType, pc.getMappedClass(), pc.getDeclaredIdentifierProperty()); } else if (!pc.isInherited() && pc.hasIdentifierProperty()) { logger.info(entityType.toString() + pc.getIdentifierProperty()); handleProperty(entityType, pc.getMappedClass(), pc.getIdentifierProperty()); } else if (pc.getIdentifier() != null) { KeyValue identifier = pc.getIdentifier(); if (identifier instanceof Component) { Component component = (Component) identifier; Iterator<?> properties = component.getPropertyIterator(); if (component.isEmbedded()) { while (properties.hasNext()) { handleProperty(entityType, pc.getMappedClass(), (org.hibernate.mapping.Property) properties.next()); } } else { String name = component.getNodeName(); Class<?> clazz = component.getType().getReturnedClass(); Type propertyType = getType(pc.getMappedClass(), clazz, name); AnnotatedElement annotated = getAnnotatedElement(pc.getMappedClass(), name); Property property = createProperty(entityType, name, propertyType, annotated); entityType.addProperty(property); // handle component properties EntityType embeddedType = createEmbeddableType(propertyType); while (properties.hasNext()) { handleProperty(embeddedType, clazz, (org.hibernate.mapping.Property) properties.next()); } } } // TODO handle other KeyValue subclasses such as Any, DependentValue and ToOne } Iterator<?> properties = pc.getDeclaredPropertyIterator(); while (properties.hasNext()) { handleProperty(entityType, pc.getMappedClass(), (org.hibernate.mapping.Property) properties.next()); } } }
From source file:com.zutubi.pulse.master.hibernate.MutableConfiguration.java
License:Apache License
public Mapping getMapping() { return new Mapping() { public IdentifierGeneratorFactory getIdentifierGeneratorFactory() { return null; }//from ww w .jav a 2s . co m public Type getIdentifierType(String persistentClass) throws MappingException { PersistentClass pc = classes.get(persistentClass); if (pc == null) { throw new MappingException(I18N.format("unknown.persistent.class", persistentClass)); } return pc.getIdentifier().getType(); } public String getIdentifierPropertyName(String persistentClass) throws MappingException { final PersistentClass pc = classes.get(persistentClass); if (pc == null) { throw new MappingException(I18N.format("unknown.persistent.class", persistentClass)); } if (!pc.hasIdentifierProperty()) return null; return pc.getIdentifierProperty().getName(); } public Type getReferencedPropertyType(String persistentClass, String propertyName) throws MappingException { final PersistentClass pc = classes.get(persistentClass); if (pc == null) { throw new MappingException(I18N.format("unknown.persistent.class", persistentClass)); } Property prop = pc.getReferencedProperty(propertyName); if (prop == null) { throw new MappingException( I18N.format("unknown.persistent.class.property", persistentClass + '.' + propertyName)); } return prop.getType(); } }; }
From source file:com.zutubi.pulse.master.transfer.jdbc.HibernateMapping.java
License:Apache License
public Type getIdentifierType(String persistentClass) throws MappingException { PersistentClass pc = configuration.getClassMapping(persistentClass); if (pc == null) { throw new MappingException("persistent class not known: " + persistentClass); }/*from www. j a v a2 s .c o m*/ return pc.getIdentifier().getType(); }
From source file:org.beangle.commons.orm.hibernate.internal.SchemaValidator.java
License:Open Source License
@SuppressWarnings({ "unchecked", "rawtypes" }) // borrow from Configuration code private Iterator<IdentifierGenerator> iterateGenerators(Configuration config, Dialect dialect) throws MappingException { TreeMap generators = new TreeMap(); String defaultCatalog = sessionFactoryBean.getHibernateProperties() .getProperty(Environment.DEFAULT_CATALOG); String defaultSchema = sessionFactoryBean.getHibernateProperties().getProperty(Environment.DEFAULT_SCHEMA); for (Iterator<PersistentClass> pcIter = config.getClassMappings(); pcIter.hasNext();) { PersistentClass pc = pcIter.next(); if (!pc.isInherited()) { IdentifierGenerator ig = pc.getIdentifier().createIdentifierGenerator( config.getIdentifierGeneratorFactory(), dialect, defaultCatalog, defaultSchema, (RootClass) pc);/*from w ww. j ava 2 s.c o m*/ if (ig instanceof PersistentIdentifierGenerator) { generators.put(((PersistentIdentifierGenerator) ig).generatorKey(), ig); } else if (ig instanceof IdentifierGeneratorAggregator) { ((IdentifierGeneratorAggregator) ig).registerPersistentGenerators(generators); } } } for (Iterator<Collection> collIter = config.getCollectionMappings(); collIter.hasNext();) { Collection collection = collIter.next(); if (collection.isIdentified()) { IdentifierGenerator ig = ((IdentifierCollection) collection).getIdentifier() .createIdentifierGenerator(config.getIdentifierGeneratorFactory(), dialect, defaultCatalog, defaultSchema, null); if (ig instanceof PersistentIdentifierGenerator) { generators.put(((PersistentIdentifierGenerator) ig).generatorKey(), ig); } } } return generators.values().iterator(); }
From source file:org.beangle.orm.hibernate.tool.DdlGenerator.java
License:Open Source License
/** * Generate sql scripts/*from ww w .j a v a 2 s . c o 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
@SuppressWarnings("unchecked") protected String buildOrderByClause(String hqlOrderBy, PersistentClass associatedClass, String role, String defaultOrder) {//from w w w. j a v a2s . c om String orderByString = null; if (hqlOrderBy != null) { List<String> properties = new ArrayList<String>(); List<String> ordering = new ArrayList<String>(); StringBuilder orderByBuffer = new StringBuilder(); if (hqlOrderBy.length() == 0) { //order by id Iterator<?> it = associatedClass.getIdentifier().getColumnIterator(); while (it.hasNext()) { Selectable col = (Selectable) it.next(); orderByBuffer.append(col.getText()).append(" asc").append(", "); } } else { StringTokenizer st = new StringTokenizer(hqlOrderBy, " ,", false); String currentOrdering = defaultOrder; //FIXME make this code decent while (st.hasMoreTokens()) { String token = st.nextToken(); if (isNonPropertyToken(token)) { if (currentOrdering != null) { throw new GrailsDomainException( "Error while parsing sort clause: " + hqlOrderBy + " (" + role + ")"); } currentOrdering = token; } else { //Add ordering of the previous if (currentOrdering == null) { //default ordering ordering.add("asc"); } else { ordering.add(currentOrdering); currentOrdering = null; } properties.add(token); } } ordering.remove(0); //first one is the algorithm starter // add last one ordering if (currentOrdering == null) { //default ordering ordering.add(defaultOrder); } else { ordering.add(currentOrdering); currentOrdering = null; } int index = 0; for (String property : properties) { Property p = BinderHelper.findPropertyByName(associatedClass, property); if (p == null) { throw new GrailsDomainException("property from sort clause not found: " + associatedClass.getEntityName() + "." + property); } PersistentClass pc = p.getPersistentClass(); String table; if (pc == null) { table = ""; } else if (pc == associatedClass || (associatedClass instanceof SingleTableSubclass && pc.getMappedClass().isAssignableFrom(associatedClass.getMappedClass()))) { table = ""; } else { table = pc.getTable().getQuotedName() + "."; } Iterator<?> propertyColumns = p.getColumnIterator(); while (propertyColumns.hasNext()) { Selectable column = (Selectable) propertyColumns.next(); orderByBuffer.append(table).append(column.getText()).append(" ").append(ordering.get(index)) .append(", "); } index++; } } orderByString = orderByBuffer.substring(0, orderByBuffer.length() - 2); } return orderByString; }