List of usage examples for org.hibernate.mapping RootClass setTable
public void setTable(Table table)
From source file:com.manydesigns.portofino.persistence.hibernate.HibernateConfig.java
License:Open Source License
protected RootClass createTableMapping(Mappings mappings, com.manydesigns.portofino.model.database.Table aTable) { Table tab = mappings.addTable(escapeName(aTable.getSchemaName()), null, escapeName(aTable.getTableName()), null, false);/*from ww w . j a v a 2 s . c o m*/ //tab.setName(escapeName(aTable.getTableName())); //tab.setSchema(escapeName(aTable.getSchemaName())); mappings.addTableBinding(aTable.getSchemaName(), null, aTable.getTableName(), aTable.getTableName(), null); RootClass clazz = new RootClass(); clazz.setEntityName(aTable.getActualEntityName()); clazz.setJpaEntityName(aTable.getActualEntityName()); if (aTable.getJavaClass() != null) { clazz.setClassName(aTable.getJavaClass()); clazz.setProxyInterfaceName(aTable.getJavaClass()); } clazz.setLazy(LAZY); clazz.setTable(tab); //clazz.setNodeName(aTable.getTableName()); List<com.manydesigns.portofino.model.database.Column> columnList = new ArrayList<com.manydesigns.portofino.model.database.Column>(); for (com.manydesigns.portofino.model.database.Column modelColumn : aTable.getColumns()) { int jdbcType = modelColumn.getJdbcType(); Class javaType = modelColumn.getActualJavaType(); //First param = null ==> doesn't really set anything, just check boolean hibernateTypeOk = setHibernateType(null, modelColumn, javaType, jdbcType); if (hibernateTypeOk) { columnList.add(modelColumn); } else { logger.error( "Cannot find Hibernate type for table: {}, column: {}, jdbc type: {}, type name: {}. Skipping column.", new Object[] { aTable.getQualifiedName(), modelColumn.getColumnName(), jdbcType, javaType != null ? javaType.getName() : null }); } } //Primary keys List<com.manydesigns.portofino.model.database.Column> columnPKList = aTable.getPrimaryKey().getColumns(); if (!columnList.containsAll(columnPKList)) { logger.error("Primary key refers to some invalid columns, skipping table {}", aTable.getQualifiedName()); return null; } if (columnPKList.size() > 1) { createPKComposite(mappings, aTable, aTable.getPrimaryKey().getPrimaryKeyName(), clazz, tab, columnPKList); } else { createPKSingle(mappings, aTable, aTable.getPrimaryKey().getPrimaryKeyName(), clazz, tab, columnPKList); } //Other columns columnList.removeAll(columnPKList); for (com.manydesigns.portofino.model.database.Column column : columnList) { Column col = createColumn(mappings, tab, column); if (col != null) { clazz.addProperty(createProperty(column, col.getValue())); } } return clazz; }
From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java
License:Apache License
protected void bindRootPersistentClassCommonValues(GrailsDomainClass domainClass, RootClass root, Mappings mappings, String sessionFactoryBeanName) { // get the schema and catalog names from the configuration Mapping m = getMapping(domainClass.getClazz()); String schema = mappings.getSchemaName(); String catalog = mappings.getCatalogName(); if (m != null) { configureDerivedProperties(domainClass, m); CacheConfig cc = m.getCache();//from www. ja v a 2 s . c om if (cc != null && cc.getEnabled()) { root.setCacheConcurrencyStrategy(cc.getUsage()); if ("read-only".equals(cc.getUsage())) { root.setMutable(false); } root.setLazyPropertiesCacheable(!"non-lazy".equals(cc.getInclude())); } Integer bs = m.getBatchSize(); if (bs != null) { root.setBatchSize(bs); } if (m.getDynamicUpdate()) { root.setDynamicUpdate(true); } if (m.getDynamicInsert()) { root.setDynamicInsert(true); } } final boolean hasTableDefinition = m != null && m.getTable() != null; if (hasTableDefinition && m.getTable().getSchema() != null) { schema = m.getTable().getSchema(); } if (hasTableDefinition && m.getTable().getCatalog() != null) { catalog = m.getTable().getCatalog(); } final boolean isAbstract = m != null && !m.getTablePerHierarchy() && m.isTablePerConcreteClass() && root.isAbstract(); // create the table Table table = mappings.addTable(schema, catalog, getTableName(domainClass, sessionFactoryBeanName), null, isAbstract); root.setTable(table); if (LOG.isDebugEnabled()) { LOG.debug("[GrailsDomainBinder] Mapping Grails domain class: " + domainClass.getFullName() + " -> " + root.getTable().getName()); } bindIdentity(domainClass, root, mappings, m, sessionFactoryBeanName); if (m == null) { bindVersion(domainClass.getVersion(), root, mappings, sessionFactoryBeanName); } else { if (m.getVersioned()) { bindVersion(domainClass.getVersion(), root, mappings, sessionFactoryBeanName); } } root.createPrimaryKey(); createClassProperties(domainClass, root, mappings, sessionFactoryBeanName); }
From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder.java
License:Apache License
private static void bindRootPersistentClassCommonValues(GrailsDomainClass domainClass, RootClass root, Mappings mappings, String sessionFactoryBeanName) { // get the schema and catalog names from the configuration Mapping m = getMapping(domainClass.getClazz()); String schema = mappings.getSchemaName(); String catalog = mappings.getCatalogName(); if (m != null) { configureDerivedProperties(domainClass, m); CacheConfig cc = m.getCache();// ww w . j a va 2s . c om if (cc != null && cc.getEnabled()) { root.setCacheConcurrencyStrategy(cc.getUsage()); if ("read-only".equals(cc.getUsage())) { root.setMutable(false); } root.setLazyPropertiesCacheable(!"non-lazy".equals(cc.getInclude())); } Integer bs = m.getBatchSize(); if (bs != null) { root.setBatchSize(bs); } if (m.getDynamicUpdate()) { root.setDynamicUpdate(true); } if (m.getDynamicInsert()) { root.setDynamicInsert(true); } } final boolean hasTableDefinition = m != null && m.getTable() != null; if (hasTableDefinition && m.getTable().getSchema() != null) { schema = m.getTable().getSchema(); } if (hasTableDefinition && m.getTable().getCatalog() != null) { catalog = m.getTable().getCatalog(); } // create the table Table table = mappings.addTable(schema, catalog, getTableName(domainClass, sessionFactoryBeanName), null, false); root.setTable(table); if (LOG.isDebugEnabled()) { LOG.debug("[GrailsDomainBinder] Mapping Grails domain class: " + domainClass.getFullName() + " -> " + root.getTable().getName()); } bindIdentity(domainClass, root, mappings, m, sessionFactoryBeanName); if (m != null) { if (m.getVersioned()) { bindVersion(domainClass.getVersion(), root, mappings, sessionFactoryBeanName); } } else { bindVersion(domainClass.getVersion(), root, mappings, sessionFactoryBeanName); } root.createPrimaryKey(); createClassProperties(domainClass, root, mappings, sessionFactoryBeanName); }
From source file:org.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java
License:Apache License
protected void bindRootPersistentClassCommonValues(HibernatePersistentEntity domainClass, RootClass root, Mappings mappings, String sessionFactoryBeanName) { // get the schema and catalog names from the configuration Mapping m = getMapping(domainClass.getJavaClass()); String schema = mappings.getSchemaName(); String catalog = mappings.getCatalogName(); if (m != null) { configureDerivedProperties(domainClass, m); CacheConfig cc = m.getCache();/* w w w. j av a2s . c o m*/ if (cc != null && cc.getEnabled()) { root.setCacheConcurrencyStrategy(cc.getUsage()); if ("read-only".equals(cc.getUsage())) { root.setMutable(false); } root.setLazyPropertiesCacheable(!"non-lazy".equals(cc.getInclude())); } Integer bs = m.getBatchSize(); if (bs != null) { root.setBatchSize(bs); } if (m.getDynamicUpdate()) { root.setDynamicUpdate(true); } if (m.getDynamicInsert()) { root.setDynamicInsert(true); } } final boolean hasTableDefinition = m != null && m.getTable() != null; if (hasTableDefinition && m.getTable().getSchema() != null) { schema = m.getTable().getSchema(); } if (hasTableDefinition && m.getTable().getCatalog() != null) { catalog = m.getTable().getCatalog(); } final boolean isAbstract = m != null && !m.getTablePerHierarchy() && m.isTablePerConcreteClass() && root.isAbstract(); // create the table Table table = mappings.addTable(schema, catalog, getTableName(domainClass, sessionFactoryBeanName), null, isAbstract); root.setTable(table); if (LOG.isDebugEnabled()) { LOG.debug("[GrailsDomainBinder] Mapping Grails domain class: " + domainClass.getName() + " -> " + root.getTable().getName()); } bindIdentity(domainClass, root, mappings, m, sessionFactoryBeanName); if (m == null) { bindVersion(domainClass.getVersion(), root, mappings, sessionFactoryBeanName); } else { if (m.getVersioned()) { bindVersion(domainClass.getVersion(), root, mappings, sessionFactoryBeanName); } } root.createPrimaryKey(); createClassProperties(domainClass, root, mappings, sessionFactoryBeanName); }
From source file:org.grails.orm.hibernate.cfg.GrailsDomainBinder.java
License:Apache License
protected void bindRootPersistentClassCommonValues(HibernatePersistentEntity domainClass, RootClass root, InFlightMetadataCollector mappings, String sessionFactoryBeanName) { // get the schema and catalog names from the configuration Mapping m = getMapping(domainClass.getJavaClass()); String schema = getSchemaName(mappings); String catalog = getCatalogName(mappings); if (m != null) { configureDerivedProperties(domainClass, m); CacheConfig cc = m.getCache();/*from w w w . j ava2s . c o m*/ if (cc != null && cc.getEnabled()) { root.setCacheConcurrencyStrategy(cc.getUsage()); if ("read-only".equals(cc.getUsage())) { root.setMutable(false); } root.setLazyPropertiesCacheable(!"non-lazy".equals(cc.getInclude())); } Integer bs = m.getBatchSize(); if (bs != null) { root.setBatchSize(bs); } if (m.getDynamicUpdate()) { root.setDynamicUpdate(true); } if (m.getDynamicInsert()) { root.setDynamicInsert(true); } } final boolean hasTableDefinition = m != null && m.getTable() != null; if (hasTableDefinition && m.getTable().getSchema() != null) { schema = m.getTable().getSchema(); } if (hasTableDefinition && m.getTable().getCatalog() != null) { catalog = m.getTable().getCatalog(); } final boolean isAbstract = m != null && !m.getTablePerHierarchy() && m.isTablePerConcreteClass() && root.isAbstract(); // create the table Table table = mappings.addTable(schema, catalog, getTableName(domainClass, sessionFactoryBeanName), null, isAbstract); root.setTable(table); if (LOG.isDebugEnabled()) { LOG.debug("[GrailsDomainBinder] Mapping Grails domain class: " + domainClass.getName() + " -> " + root.getTable().getName()); } bindIdentity(domainClass, root, mappings, m, sessionFactoryBeanName); if (m == null) { bindVersion(domainClass.getVersion(), root, mappings, sessionFactoryBeanName); } else { if (m.getVersioned()) { bindVersion(domainClass.getVersion(), root, mappings, sessionFactoryBeanName); } } root.createPrimaryKey(); createClassProperties(domainClass, root, mappings, sessionFactoryBeanName); }
From source file:org.openflexo.jdbc.hbn.MyDBTest.java
License:Open Source License
@Test @TestOrder(5)/* ww w . ja v a 2 s . co m*/ public void testDeclareJDBCMapping() { MetadataBuildingContextRootImpl metadataBuildingContext = new MetadataBuildingContextRootImpl( buildingOptions, new ClassLoaderAccessImpl(null, config.getServiceRegistry()), metadataCollector); metadata = metadataCollector.buildMetadataInstance(metadataBuildingContext); // ************************ // Creation de l'entit persiste "Dynamic_Class" RootClass pClass = new RootClass(metadataBuildingContext); pClass.setEntityName("Dynamic_Class"); pClass.setJpaEntityName("Dynamic_Class"); pClass.setTable(table); metadataCollector.addEntityBinding(pClass); // Creation d'une proprit (clef) et son mapping Property prop = new Property(); prop.setName("Nom"); SimpleValue value = new SimpleValue((MetadataImplementor) metadata, table); value.setTypeName("java.lang.String"); value.setIdentifierGeneratorStrategy("assigned"); value.addColumn(col); value.setTable(table); prop.setValue(value); pClass.setDeclaredIdentifierProperty(prop); pClass.setIdentifierProperty(prop); pClass.setIdentifier(value); // Creation d'une proprit et son mapping prop = new Property(); prop.setName("Prenom"); value = new SimpleValue((MetadataImplementor) metadata, table); value.setTypeName(String.class.getCanonicalName()); value.addColumn(col2); value.setTable(table); prop.setValue(value); pClass.addProperty(prop); // ************************ // Creation de l'entit persiste "Adresse" RootClass pClass2 = new RootClass(metadataBuildingContext); pClass2.setEntityName("Adresse"); pClass2.setJpaEntityName("Adresse"); pClass2.setTable(table2); metadataCollector.addEntityBinding(pClass2); // Creation d'une proprit (clef) et son mapping prop = new Property(); prop.setName("Identifiant"); value = new SimpleValue((MetadataImplementor) metadata, table2); value.setTypeName("java.lang.Integer"); value.setIdentifierGeneratorStrategy("native"); value.addColumn(col4); value.setTable(table2); prop.setValue(value); pClass2.setDeclaredIdentifierProperty(prop); pClass2.setIdentifierProperty(prop); pClass2.setIdentifier(value); // Creation d'une proprit et son mapping prop = new Property(); prop.setName("Prenom"); value = new SimpleValue((MetadataImplementor) metadata, table2); value.setTypeName(String.class.getCanonicalName()); value.addColumn(col5); value.setTable(table2); prop.setValue(value); pClass2.addProperty(prop); try { ((MetadataImplementor) metadata).validate(); } catch (MappingException e) { System.out.println("Validation Error: " + e.getMessage()); } Namespace namespace = metadataCollector.getDatabase().getDefaultNamespace(); for (JDBCTable aTable : connection.getSchema().getTables()) { System.out.println("Found table: " + aTable + " hop: " + aTable.getName()); Identifier logicalName = metadataCollector.getDatabase().toIdentifier(aTable.getName()); System.out.println("logicalName=" + logicalName); Table laTable = namespace.locateTable(logicalName); System.out.println("latable=" + laTable); } }
From source file:org.openflexo.technologyadapter.jdbc.model.DynamicModelBuilder.java
License:Open Source License
public Metadata buildDynamicModel() { Metadata metadata = metadataCollector.buildMetadataInstance(metadataBuildingContext); Database database = metadata.getDatabase(); // ********** // Creation / Dfinition de la table T_Dynamic_Table Table table = metadataCollector.addTable("", "", "T_Dynamic_Table", null, false); table.setName("T_Dynamic_Table"); Column col = new Column(); col.setName("pouet"); col.setLength(256);/*w ww . j a v a2 s. c om*/ col.setSqlType("CHAR(256)"); col.setNullable(false); table.addColumn(col); PrimaryKey pk = new PrimaryKey(table); pk.addColumn(col); UniqueKey uk1 = new UniqueKey(); uk1.setName("Nom_Unique"); uk1.setTable(table); uk1.addColumn(col); table.addUniqueKey(uk1); Column col2 = new Column(); col2.setName("padam"); col2.setLength(256); col2.setSqlType("CHAR(256)"); col2.setNullable(true); table.addColumn(col2); // pour rire les couples "Nom + Prenom" doivent tre uniques UniqueKey uk = new UniqueKey(); uk.setName("Couple_Nom_Prenom_Unique"); uk.setTable(table); uk.addColumn(col); uk.addColumn(col2); table.addUniqueKey(uk); // une colonne de clef etrangre vers T_Adresse Column col3 = new Column(); col3.setName("id_addr"); col3.setLength(16); col3.setSqlType("INTEGER"); col3.setNullable(true); table.addColumn(col3); // ********** // Creation / Dfinition de la table T_Adresse Table table2 = metadataCollector.addTable("", "", "T_Adresse", null, false); table2.setName("T_Adresse"); Column col4 = new Column(); col4.setName("Id"); col4.setLength(16); col4.setSqlType("INTEGER"); col4.setNullable(false); table2.addColumn(col4); pk = new PrimaryKey(table2); pk.addColumn(col); uk1 = new UniqueKey(); uk1.setName("Id_Unique"); uk1.setTable(table2); uk1.addColumn(col4); table.addUniqueKey(uk1); Column col5 = new Column(); col5.setName("Adresse"); col5.setLength(512); col5.setSqlType("CHAR(512)"); col5.setNullable(true); table2.addColumn(col5); // ************************ // Creation de l'entit persiste "Dynamic_Class" RootClass pClass = new RootClass(metadataBuildingContext); pClass.setEntityName("Dynamic_Class"); pClass.setJpaEntityName("Dynamic_Class"); pClass.setTable(table); metadataCollector.addEntityBinding(pClass); // Creation d'une proprit (clef) et son mapping Property prop = new Property(); prop.setName("Nom"); SimpleValue value = new SimpleValue((MetadataImplementor) metadata, table); value.setTypeName("java.lang.String"); value.setIdentifierGeneratorStrategy("assigned"); value.addColumn(col); value.setTable(table); prop.setValue(value); pClass.setDeclaredIdentifierProperty(prop); pClass.setIdentifierProperty(prop); pClass.setIdentifier(value); // Creation d'une proprit et son mapping prop = new Property(); prop.setName("Prenom"); value = new SimpleValue((MetadataImplementor) metadata, table); value.setTypeName(String.class.getCanonicalName()); value.addColumn(col2); value.setTable(table); prop.setValue(value); pClass.addProperty(prop); // ************************ // Creation de l'entit persiste "Adresse" RootClass pClass2 = new RootClass(metadataBuildingContext); pClass2.setEntityName("Adresse"); pClass2.setJpaEntityName("Adresse"); pClass2.setTable(table2); metadataCollector.addEntityBinding(pClass2); // Creation d'une proprit (clef) et son mapping prop = new Property(); prop.setName("Identifiant"); value = new SimpleValue((MetadataImplementor) metadata, table2); value.setTypeName("java.lang.Integer"); value.setIdentifierGeneratorStrategy("native"); value.addColumn(col4); value.setTable(table2); prop.setValue(value); pClass2.setDeclaredIdentifierProperty(prop); pClass2.setIdentifierProperty(prop); pClass2.setIdentifier(value); // Creation d'une proprit et son mapping prop = new Property(); prop.setName("Prenom"); value = new SimpleValue((MetadataImplementor) metadata, table2); value.setTypeName(String.class.getCanonicalName()); value.addColumn(col5); value.setTable(table2); prop.setValue(value); pClass2.addProperty(prop); try { ((MetadataImplementor) metadata).validate(); } catch (MappingException e) { System.out.println("Validation Error: " + e.getMessage()); } return metadata; }