List of usage examples for org.hibernate.mapping Column setNullable
public void setNullable(boolean nullable)
From source file:com.manydesigns.portofino.persistence.hibernate.HibernateConfig.java
License:Open Source License
protected Column createColumn(Mappings mappings, Table tab, com.manydesigns.portofino.model.database.Column column) { Column col = new Column(); col.setName(escapeName(column.getColumnName())); col.setLength(column.getLength());//from w ww . j a v a 2 s . c o m col.setPrecision(column.getLength()); col.setScale(column.getScale()); col.setNullable(column.isNullable()); String columnType = column.getColumnType(); int jdbcType = column.getJdbcType(); col.setSqlTypeCode(jdbcType); col.setSqlType(columnType); SimpleValue value = new SimpleValue(mappings, tab); if (!setHibernateType(value, column, column.getActualJavaType(), jdbcType)) { logger.error("Skipping column {}", column.getQualifiedName()); return null; } value.addColumn(col); tab.addColumn(col); mappings.addColumnBinding(column.getColumnName(), col, tab); return col; }
From source file:net.e6tech.elements.persist.hibernate.ModifiedTableGenerator.java
License:Apache License
@Override public void registerExportables(Database database) { final Dialect dialect = database.getJdbcEnvironment().getDialect(); final Namespace namespace = database.locateNamespace(qualifiedTableName.getCatalogName(), qualifiedTableName.getSchemaName()); Table table = namespace.locateTable(qualifiedTableName.getObjectName()); if (table == null) { table = namespace.createTable(qualifiedTableName.getObjectName(), false); // todo : note sure the best solution here. do we add the columns if missing? other? final Column segmentColumn = new ExportableColumn(database, table, segmentColumnName, StringType.INSTANCE, dialect.getTypeName(Types.VARCHAR, segmentValueLength, 0, 0)); segmentColumn.setNullable(false); table.addColumn(segmentColumn);// ww w .ja v a2s. c o m // lol table.setPrimaryKey(new PrimaryKey(table)); table.getPrimaryKey().addColumn(segmentColumn); final Column valueColumn = new ExportableColumn(database, table, valueColumnName, LongType.INSTANCE); table.addColumn(valueColumn); } // allow physical naming strategies a chance to kick in this.renderedTableName = database.getJdbcEnvironment().getQualifiedObjectNameFormatter() .format(table.getQualifiedTableName(), dialect); this.selectQuery = buildSelectQuery(dialect); this.updateQuery = buildUpdateQuery(); this.insertQuery = buildInsertQuery(); }
From source file:net.lshift.hibernate.migrations.AlterTableBuilder.java
License:Apache License
private static Column buildColumnDefinition(String name, int sqlType, int length, boolean nullable, Object defaultVal) {/*from w w w . j a va 2 s. c o m*/ Column col = new Column(name); col.setSqlTypeCode(sqlType); col.setNullable(nullable); col.setLength(length); col.setDefaultValue(defaultVal != null ? defaultVal.toString() : null); return col; }
From source file:net.lshift.hibernate.migrations.CreateTableBuilder.java
License:Apache License
public CreateTableBuilder column(String name, int sqlType, int length, boolean nullable, Object defaultVal) { Column col = new Column(name); col.setNullable(nullable); col.setSqlTypeCode(sqlType);//from w w w . j ava2 s. c o m col.setLength(length); col.setDefaultValue(defaultVal != null ? defaultVal.toString() : null); columns.add(col); return this; }
From source file:org.apereo.portal.tools.dbloader.TableXmlHandler.java
License:Apache License
@Override public void endElement(String uri, String localName, String name) throws SAXException { if ("table".equals(name)) { for (final Column column : this.currentColumns.values()) { this.currentTable.addColumn(column); }//from ww w . j av a 2 s. c o m if (this.primaryKey != null) { this.currentTable.setPrimaryKey(this.primaryKey); } this.tables.put(this.currentTable.getName(), this.currentTable); this.tableColumnTypes.put(this.currentTable.getName(), this.currentColumnTypes); this.primaryKey = null; this.currentColumns = null; this.currentColumnTypes = null; this.currentTable = null; } else if ("column".equals(name)) { this.currentColumns.put(this.currentColumn.getName(), this.currentColumn); this.currentColumn = null; } else if ("name".equals(name)) { final String itemName = this.chars.toString().trim(); if (this.currentIndex != null) { this.currentIndex.setName(itemName); } else if (this.currentUnique != null) { this.currentUnique.setName(itemName); } else if (this.currentTable == null) { this.currentTable = new Table(itemName); } else if (this.currentColumn == null) { this.currentColumn = new Column(itemName); } } else if ("type".equals(name)) { final String sqlTypeName = this.chars.toString().trim(); final int sqlType = this.getSqlType(sqlTypeName); this.currentColumnTypes.put(this.currentColumn.getName(), sqlType); final String hibType = this.getHibernateType(sqlType); final SimpleValue value = new SimpleValue(this.mappings, this.currentTable); value.setTypeName(hibType); this.currentColumn.setValue(value); } else if ("param".equals(name)) { final String param = this.chars.toString().trim(); final Integer length = Integer.valueOf(param); this.currentColumn.setLength(length); } else if ("primary-key".equals(name)) { final String columnName = this.chars.toString().trim(); if (this.primaryKey == null) { this.primaryKey = new PrimaryKey(); } final Column column = this.currentColumns.get(columnName); this.primaryKey.addColumn(column); } else if ("not-null".equals(name)) { final String columnName = this.chars.toString().trim(); final Column column = this.currentColumns.get(columnName); column.setNullable(false); } else if ("column-ref".equals(name)) { final String columnName = this.chars.toString().trim(); final Column column = this.currentColumns.get(columnName); if (this.currentIndex != null) { this.currentIndex.addColumn(column); } else if (this.currentUnique != null) { this.currentUnique.addColumn(column); } } else if ("index".equals(name)) { this.currentTable.addIndex(this.currentIndex); this.currentIndex = null; } else if ("unique".equals(name)) { this.currentTable.addUniqueKey(this.currentUnique); this.currentUnique = null; } else if ("key".equals(name)) { this.logger.warn("the 'key' element is ignored, use the table level 'primary-key' element instead"); } this.chars = null; }
From source file:org.beangle.orm.hibernate.tool.HbmGenerator.java
License:Open Source License
@SuppressWarnings("unchecked") public void gen(String file) throws Exception { hbconfig = new OverrideConfiguration(); hbconfig.getProperties().put(Environment.DIALECT, new Oracle10gDialect()); ConfigBuilder.build(hbconfig);/* w w w. j a v a 2 s. co m*/ freemarkerConfig = new freemarker.template.Configuration(); freemarkerConfig.setTemplateLoader(new ClassTemplateLoader(getClass(), "/")); Iterator<PersistentClass> iter = hbconfig.getClassMappings(); List<PersistentClass> pcs = CollectUtils.newArrayList(); while (iter.hasNext()) { PersistentClass pc = iter.next(); Class<?> cls = pc.getMappedClass(); Iterator<Property> pi = pc.getPropertyIterator(); // For AnnotationBinder don't set column'length and nullable in ,let's we do it. while (pi.hasNext()) { Property p = pi.next(); if (p.getColumnSpan() != 1) continue; Column column = (Column) p.getColumnIterator().next(); if (column.getLength() == Column.DEFAULT_LENGTH) { Size size = findAnnotation(cls, Size.class, p.getName()); if (null != size) column.setLength(size.max()); } if (column.isNullable()) { NotNull notnull = findAnnotation(cls, NotNull.class, p.getName()); if (null != notnull) column.setNullable(false); } } if (!pc.getClassName().contains(".example.")) pcs.add(pc); } Map<String, Object> data = CollectUtils.newHashMap(); data.put("classes", pcs); data.put("generator", this); Template freemarkerTemplate = freemarkerConfig.getTemplate("/hbm.ftl"); FileWriter fw = new FileWriter("/tmp/hibernate.hbm.xml"); freemarkerTemplate.process(data, fw); }
From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java
License:Apache License
protected void linkValueUsingAColumnCopy(GrailsDomainClassProperty prop, Column column, DependantValue key) { Column mappingColumn = new Column(); mappingColumn.setName(column.getName()); mappingColumn.setLength(column.getLength()); mappingColumn.setNullable(prop.isOptional()); mappingColumn.setSqlType(column.getSqlType()); mappingColumn.setValue(key);/* w ww. jav a2 s .co m*/ key.addColumn(mappingColumn); key.getTable().addColumn(mappingColumn); }
From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java
License:Apache License
protected void bindEnumType(GrailsDomainClassProperty property, Class<?> propertyType, SimpleValue simpleValue, String columnName) {/*from ww w. j a v a 2 s . co m*/ PropertyConfig pc = getPropertyConfig(property); String typeName = getTypeName(property, getPropertyConfig(property), getMapping(property.getDomainClass())); if (typeName == null) { Properties enumProperties = new Properties(); enumProperties.put(ENUM_CLASS_PROP, propertyType.getName()); String enumType = pc == null ? DEFAULT_ENUM_TYPE : pc.getEnumType(); if (enumType.equals(DEFAULT_ENUM_TYPE) && identityEnumTypeSupports(propertyType)) { simpleValue.setTypeName("org.codehaus.groovy.grails.orm.hibernate.cfg.IdentityEnumType"); } else { simpleValue.setTypeName(ENUM_TYPE_CLASS); if (enumType.equals(DEFAULT_ENUM_TYPE) || "string".equalsIgnoreCase(enumType)) { enumProperties.put(ENUM_TYPE_PROP, String.valueOf(Types.VARCHAR)); } else if (!"ordinal".equalsIgnoreCase(enumType)) { LOG.warn("Invalid enumType specified when mapping property [" + property.getName() + "] of class [" + property.getDomainClass().getClazz().getName() + "]. Using defaults instead."); } } simpleValue.setTypeParameters(enumProperties); } else { simpleValue.setTypeName(typeName); } Table t = simpleValue.getTable(); Column column = new Column(); if (property.getDomainClass().isRoot()) { column.setNullable(property.isOptional()); } else { Mapping mapping = getMapping(property.getDomainClass()); if (mapping == null || mapping.getTablePerHierarchy()) { if (LOG.isDebugEnabled()) { LOG.debug("[GrailsDomainBinder] Sub class property [" + property.getName() + "] for column name [" + column.getName() + "] set to nullable"); } column.setNullable(true); } else { column.setNullable(property.isOptional()); } } column.setValue(simpleValue); column.setName(columnName); if (t != null) t.addColumn(column); simpleValue.addColumn(column); PropertyConfig propertyConfig = getPropertyConfig(property); if (propertyConfig != null && !propertyConfig.getColumns().isEmpty()) { bindIndex(columnName, column, propertyConfig.getColumns().get(0), t); bindColumnConfigToColumn(column, propertyConfig.getColumns().get(0)); } }
From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java
License:Apache License
protected void bindComponentProperty(Component component, GrailsDomainClassProperty componentProperty, GrailsDomainClassProperty currentGrailsProp, PersistentClass persistentClass, String path, Table table, Mappings mappings, String sessionFactoryBeanName) { Value value;/* ww w . j a v a 2 s . co m*/ // see if it's a collection type CollectionType collectionType = CT.collectionTypeForClass(currentGrailsProp.getType()); if (collectionType != null) { // create collection Collection collection = collectionType.create(currentGrailsProp, persistentClass, path, mappings, sessionFactoryBeanName); mappings.addCollection(collection); value = collection; } // 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, path, mappings, sessionFactoryBeanName); } else if (currentGrailsProp.isOneToOne()) { if (LOG.isDebugEnabled()) LOG.debug( "[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName() + "] as OneToOne"); if (canBindOneToOneWithSingleColumnAndForeignKey(currentGrailsProp)) { value = new OneToOne(mappings, table, persistentClass); bindOneToOne(currentGrailsProp, (OneToOne) value, path, sessionFactoryBeanName); } else { value = new ManyToOne(mappings, table); bindManyToOne(currentGrailsProp, (ManyToOne) value, 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); if (currentGrailsProp.isEnum()) { bindEnumType(currentGrailsProp, (SimpleValue) value, path, sessionFactoryBeanName); } else { bindSimpleValue(currentGrailsProp, componentProperty, (SimpleValue) value, path, mappings, sessionFactoryBeanName); } } if (value != null) { Property persistentProperty = createProperty(value, persistentClass, currentGrailsProp, mappings); component.addProperty(persistentProperty); if (isComponentPropertyNullable(componentProperty)) { final Iterator<?> columnIterator = value.getColumnIterator(); while (columnIterator.hasNext()) { Column c = (Column) columnIterator.next(); c.setNullable(true); } } } }
From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java
License:Apache License
/** * Binds a value for the specified parameters to the meta model. * * @param type The type of the property * @param simpleValue The simple value instance * @param nullable Whether it is nullable * @param columnName The property name//from www.j a v a 2 s . co m * @param mappings The mappings */ protected void bindSimpleValue(String type, SimpleValue simpleValue, boolean nullable, String columnName, Mappings mappings) { simpleValue.setTypeName(type); Table t = simpleValue.getTable(); Column column = new Column(); column.setNullable(nullable); column.setValue(simpleValue); column.setName(columnName); if (t != null) t.addColumn(column); simpleValue.addColumn(column); }