List of usage examples for org.hibernate.mapping Table setComment
public void setComment(String comment)
From source file:com.vecna.maven.hibernate.HibernateDocMojo.java
License:Apache License
/** * Populate Hibernate properties with comments from javadocs (including nested properties). * @param propertyIterator iterator over top-level properties * @param accumulatedJavadoc comments accumulated so far (for nested properties) *//* ww w.j a v a 2 s . c o m*/ private void processProperties(Iterator<Property> propertyIterator, Class<?> cls, JavaDocBuilder javaDocs, String accumulatedJavadoc) { JavaClass javaClass = javaDocs.getClassByName(cls.getName()); if (javaClass != null) { while (propertyIterator.hasNext()) { Property prop = propertyIterator.next(); Value value = prop.getValue(); if (value instanceof Collection) { Collection collection = (Collection) value; Value elementValue = collection.getElement(); if (elementValue instanceof Component) { processComponent((Component) elementValue, javaDocs, accumulatedJavadoc); } Table collectionTable = collection.getCollectionTable(); if (collectionTable.getComment() == null) { collectionTable.setComment(getSimpleValueJavadoc(prop, cls, javaClass)); } } else if (value instanceof Component) { String comment = getSimpleValueJavadoc(prop, cls, javaClass); comment = accumulateJavadoc(comment, accumulatedJavadoc); processComponent((Component) value, javaDocs, comment); } else if (value instanceof SimpleValue) { String comment = getSimpleValueJavadoc(prop, cls, javaClass); comment = accumulateJavadoc(comment, accumulatedJavadoc); setComment(comment, prop); } } } }
From source file:com.vecna.maven.hibernate.HibernateDocMojo.java
License:Apache License
/** * Populate table/column comments in a Hibernate model from javadocs *//*w ww . j a va 2 s .com*/ private void populateCommentsFromJavadocs(Configuration configuration, JavaDocBuilder javaDocs) { Iterator<PersistentClass> mappedClasses = configuration.getClassMappings(); while (mappedClasses.hasNext()) { PersistentClass mappedClass = mappedClasses.next(); Table table = mappedClass.getTable(); JavaClass javaClass = javaDocs.getClassByName(mappedClass.getClassName()); if (javaClass != null) { if (table != null) { String comment = javaClass.getComment(); if (mappedClass.getDiscriminator() != null) { String newComment = "Discriminator '" + mappedClass.getDiscriminatorValue() + "': " + comment; if (table.getComment() != null) { newComment = table.getComment() + "<br><br>" + newComment; } table.setComment(newComment); @SuppressWarnings("unchecked") Iterator<Column> discriminatorColumns = mappedClass.getDiscriminator().getColumnIterator(); setComment("discriminator - see table comment", discriminatorColumns); } else { table.setComment(comment); } } @SuppressWarnings("unchecked") Iterator<Property> propertyIterator = mappedClass.getPropertyIterator(); processProperties(propertyIterator, mappedClass.getMappedClass(), javaDocs, null); } if (mappedClass.getIdentifierProperty() != null) { setComment("Primary key", mappedClass.getIdentifierProperty()); } } }
From source file:com.zutubi.pulse.master.hibernate.SchemaRefactor.java
License:Apache License
protected Table clone(Table table) { Table clone = new Table(table.getName()); clone.setAbstract(table.isAbstract()); clone.setCatalog(table.getCatalog()); clone.setComment(table.getComment()); clone.setName(table.getName());/* w w w. j a v a2 s.co m*/ clone.setPrimaryKey(table.getPrimaryKey()); clone.setQuoted(table.isQuoted()); clone.setRowId(table.getRowId()); clone.setSchema(table.getSchema()); clone.setSubselect(table.getSubselect()); Iterator columns = table.getColumnIterator(); while (columns.hasNext()) { Column column = (Column) columns.next(); clone.addColumn(column); } Iterator foreignKeys = table.getForeignKeyIterator(); while (foreignKeys.hasNext()) { ForeignKey key = (ForeignKey) foreignKeys.next(); clone.createForeignKey(key.getName(), key.getColumns(), key.getReferencedEntityName(), key.getReferencedColumns()); } return clone; }
From source file:org.beangle.orm.hibernate.tool.DdlGenerator.java
License:Open Source License
/** * Generate sql scripts/* ww w. jav a 2 s . 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
/** * 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 *//*ww w.j a va 2 s . c o m*/ protected void createClassProperties(GrailsDomainClass domainClass, PersistentClass persistentClass, Mappings mappings, String sessionFactoryBeanName) { GrailsDomainClassProperty[] persistentProperties = domainClass.getPersistentProperties(); Table table = persistentClass.getTable(); Mapping gormMapping = getMapping(domainClass.getClazz()); if (gormMapping != null) { table.setComment(gormMapping.getComment()); } for (GrailsDomainClassProperty currentGrailsProp : persistentProperties) { // if its inherited skip boolean isBidirectionalManyToOne = isBidirectionalManyToOne(currentGrailsProp); if (currentGrailsProp.isInherited()) { continue; } if (currentGrailsProp.isInherited() && isBidirectionalManyToOne && currentGrailsProp.isCircular()) { 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.isOptional(), getColumnNameForPropertyAndPath(currentGrailsProp, EMPTY_PATH, null, sessionFactoryBeanName), mappings); } else { // create collection Collection collection = collectionType.create(currentGrailsProp, persistentClass, EMPTY_PATH, mappings, sessionFactoryBeanName); mappings.addCollection(collection); value = collection; } } else if (currentGrailsProp.isEnum()) { value = new SimpleValue(mappings, table); bindEnumType(currentGrailsProp, (SimpleValue) value, EMPTY_PATH, sessionFactoryBeanName); } // work out what type of relationship it is and bind value else if (currentGrailsProp.isManyToOne()) { if (LOG.isDebugEnabled()) LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName() + "] as ManyToOne"); value = new ManyToOne(mappings, table); bindManyToOne(currentGrailsProp, (ManyToOne) value, EMPTY_PATH, mappings, sessionFactoryBeanName); } else if (currentGrailsProp.isOneToOne() && userType == null) { if (LOG.isDebugEnabled()) LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName() + "] as OneToOne"); if (currentGrailsProp.isHasOne() && !currentGrailsProp.isBidirectional()) { throw new MappingException("hasOne property [" + currentGrailsProp.getDomainClass().getName() + "." + currentGrailsProp.getName() + "] is not bidirectional. Specify the other side of the relationship!"); } else if (canBindOneToOneWithSingleColumnAndForeignKey(currentGrailsProp)) { value = new OneToOne(mappings, table, persistentClass); bindOneToOne(currentGrailsProp, (OneToOne) value, EMPTY_PATH, sessionFactoryBeanName); } else { if (currentGrailsProp.isHasOne() && currentGrailsProp.isBidirectional()) { value = new OneToOne(mappings, table, persistentClass); bindOneToOne(currentGrailsProp, (OneToOne) value, EMPTY_PATH, sessionFactoryBeanName); } else { value = new ManyToOne(mappings, table); bindManyToOne(currentGrailsProp, (ManyToOne) value, EMPTY_PATH, mappings, sessionFactoryBeanName); } } } else if (currentGrailsProp.isEmbedded()) { value = new Component(mappings, persistentClass); bindComponent((Component) value, currentGrailsProp, true, mappings, sessionFactoryBeanName); } 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); } } bindNaturalIdentifier(table, gormMapping, persistentClass); }
From source file:org.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.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 *///from ww w . j a va 2s.c o m protected void createClassProperties(HibernatePersistentEntity domainClass, PersistentClass persistentClass, Mappings 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.addCollection(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
/** * 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 *//*www . ja va2 s . c o m*/ 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); }