List of usage examples for org.hibernate.mapping UniqueKey setTable
public void setTable(Table table)
From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java
License:Apache License
protected void bindNaturalIdentifier(Table table, Mapping mapping, PersistentClass persistentClass) { Object o = mapping != null ? mapping.getIdentity() : null; if (!(o instanceof Identity)) { return;//from w w w. ja v a 2s. c om } Identity identity = (Identity) o; final NaturalId naturalId = identity.getNatural(); if (naturalId == null || naturalId.getPropertyNames().isEmpty()) { return; } UniqueKey uk = new UniqueKey(); uk.setTable(table); boolean mutable = naturalId.isMutable(); for (String propertyName : naturalId.getPropertyNames()) { Property property = persistentClass.getProperty(propertyName); property.setNaturalIdentifier(true); if (!mutable) property.setUpdateable(false); uk.addColumns(property.getColumnIterator()); } setUniqueName(uk); table.addUniqueKey(uk); }
From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder.java
License:Apache License
private static void bindNaturalIdentifier(Table table, Mapping mapping, PersistentClass persistentClass) { Object o = mapping != null ? mapping.getIdentity() : null; if (o instanceof Identity) { Identity identity = (Identity) o; final NaturalId naturalId = identity.getNatural(); if (naturalId != null && !naturalId.getPropertyNames().isEmpty()) { UniqueKey uk = new UniqueKey(); uk.setName("_UniqueKey"); uk.setTable(table); boolean mutable = naturalId.isMutable(); for (String propertyName : naturalId.getPropertyNames()) { Property property = persistentClass.getProperty(propertyName); property.setNaturalIdentifier(true); if (!mutable) property.setUpdateable(false); uk.addColumns(property.getColumnIterator()); }/* w ww . j a va2 s .com*/ table.addUniqueKey(uk); } } }
From source file:org.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java
License:Apache License
protected void bindNaturalIdentifier(Table table, Mapping mapping, PersistentClass persistentClass) { Object o = mapping != null ? mapping.getIdentity() : null; if (!(o instanceof Identity)) { return;// w w w .j a va 2s. c o m } Identity identity = (Identity) o; final NaturalId naturalId = identity.getNatural(); if (naturalId == null || naturalId.getPropertyNames().isEmpty()) { return; } UniqueKey uk = new UniqueKey(); uk.setTable(table); boolean mutable = naturalId.isMutable(); for (String propertyName : naturalId.getPropertyNames()) { Property property = persistentClass.getProperty(propertyName); property.setNaturalIdentifier(true); if (!mutable) property.setUpdateable(false); uk.addColumns(property.getColumnIterator()); } setGeneratedUniqueName(uk); table.addUniqueKey(uk); }
From source file:org.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java
License:Apache License
protected void createUniqueKeyForColumns(Table table, String columnName, List<Column> columns) { Collections.reverse(columns); UniqueKey uk = new UniqueKey(); uk.setTable(table); uk.addColumns(columns.iterator());//ww w . jav a 2 s . c o m if (LOG.isDebugEnabled()) { LOG.debug("create unique key for " + table.getName() + " columns = " + columns); } setGeneratedUniqueName(uk); table.addUniqueKey(uk); }
From source file:org.openflexo.jdbc.hbn.MyDBTest.java
License:Open Source License
@Test @TestOrder(4)/* w ww. j a va 2 s.co m*/ public void testDeclareJDBCMetaData() { table = metadataCollector.addTable("", "", "T_Dynamic_Table_2", null, false); table.setName("T_Dynamic_Table_2"); col = new Column(); col.setName("pouet"); col.setLength(256); col.setSqlType("CHAR(32)"); 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); col2 = new Column(); col2.setName("padam"); col2.setLength(256); col2.setSqlType("CHAR(32)"); 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 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 table2 = metadataCollector.addTable("", "", "T_Adresse_2", null, false); table2.setName("T_Adresse_2"); 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); col5 = new Column(); col5.setName("Adresse"); col5.setLength(512); col5.setSqlType("CHAR(512)"); col5.setNullable(true); table2.addColumn(col5); }
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);//from ww w.j a v a 2s .c o m 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; }