use of org.hibernate.mapping.PrimaryKey in project uPortal by Jasig.
the class TableXmlHandler method endElement.
/* (non-Javadoc)
* @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
*/
@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);
}
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;
}
use of org.hibernate.mapping.PrimaryKey in project hibernate-orm by hibernate.
the class MultipleHiLoPerTableGenerator method registerExportables.
@Override
public void registerExportables(Database database) {
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?
table.setPrimaryKey(new PrimaryKey(table));
final Column pkColumn = new ExportableColumn(database, table, segmentColumnName, StringType.INSTANCE, database.getDialect().getTypeName(Types.VARCHAR, keySize, 0, 0));
pkColumn.setNullable(false);
table.addColumn(pkColumn);
table.getPrimaryKey().addColumn(pkColumn);
final Column valueColumn = new ExportableColumn(database, table, valueColumnName, LongType.INSTANCE);
table.addColumn(valueColumn);
}
final JdbcEnvironment jdbcEnvironment = database.getJdbcEnvironment();
// allow physical naming strategies a chance to kick in
tableName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(table.getQualifiedTableName(), jdbcEnvironment.getDialect());
query = "select " + valueColumnName + " from " + jdbcEnvironment.getDialect().appendLockHint(LockMode.PESSIMISTIC_WRITE, tableName) + " where " + segmentColumnName + " = '" + segmentName + "'" + jdbcEnvironment.getDialect().getForUpdateString();
update = "update " + tableName + " set " + valueColumnName + " = ? where " + valueColumnName + " = ? and " + segmentColumnName + " = '" + segmentName + "'";
insert = "insert into " + tableName + "(" + segmentColumnName + ", " + valueColumnName + ") " + "values('" + segmentName + "', ?)";
}
use of org.hibernate.mapping.PrimaryKey in project xwiki-platform by xwiki.
the class R40000XWIKI6990DataMigration method appendAddPrimaryKey.
/**
* Append a add primary key constraint command for the given table.
*
* @param sb append the result into this string builder
* @param table the table name
*/
private void appendAddPrimaryKey(StringBuilder sb, Table table) {
PrimaryKey pk = table.getPrimaryKey();
String pkName = pk.getName();
sb.append(" <addPrimaryKey tableName=\"").append(table.getName()).append("\" columnNames=\"");
@SuppressWarnings("unchecked") Iterator<Column> columns = pk.getColumnIterator();
while (columns.hasNext()) {
Column column = columns.next();
sb.append(column.getName());
if (columns.hasNext()) {
sb.append(",");
}
}
if (pkName != null) {
sb.append("\" constraintName=\"").append(pkName);
}
sb.append("\"/>\n");
}
use of org.hibernate.mapping.PrimaryKey in project jbosstools-hibernate by jbosstools.
the class TableFacadeTest method testGetPrimaryKey.
@Test
public void testGetPrimaryKey() {
PrimaryKey primaryKey = new PrimaryKey();
Table table = new Table();
ITable tableFacade = FACADE_FACTORY.createTable(table);
Assert.assertNull(tableFacade.getPrimaryKey());
table.setPrimaryKey(primaryKey);
IPrimaryKey primaryKeyFacade = tableFacade.getPrimaryKey();
Assert.assertSame(primaryKey, ((IFacade) primaryKeyFacade).getTarget());
}
use of org.hibernate.mapping.PrimaryKey in project jbosstools-hibernate by jbosstools.
the class FacadeFactoryTest method testCreateHQLQueryPlan.
@Test
public void testCreateHQLQueryPlan() {
final Collection<PersistentClass> entityBindings = new ArrayList<PersistentClass>();
final StandardServiceRegistryBuilder standardServiceRegistryBuilder = new StandardServiceRegistryBuilder();
standardServiceRegistryBuilder.applySetting(AvailableSettings.DIALECT, TestDialect.class.getName());
final StandardServiceRegistry serviceRegistry = standardServiceRegistryBuilder.build();
final MetadataSources metadataSources = new MetadataSources(serviceRegistry);
final MetadataImplementor metadata = (MetadataImplementor) metadataSources.buildMetadata();
Table t = new Table("FOO");
Column c = new Column("foo");
t.addColumn(c);
PrimaryKey key = new PrimaryKey(t);
key.addColumn(c);
t.setPrimaryKey(key);
SimpleValue sv = new SimpleValue(metadata);
sv.setNullValue("null");
sv.setTypeName(Integer.class.getName());
sv.setTable(t);
sv.addColumn(c);
final RootClass rc = new RootClass(null);
rc.setEntityName("foo");
rc.setJpaEntityName("foo");
rc.setIdentifier(sv);
rc.setTable(t);
entityBindings.add(rc);
MetadataImplementor wrapper = (MetadataImplementor) Proxy.newProxyInstance(facadeFactory.getClassLoader(), new Class[] { MetadataImplementor.class }, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if ("getEntityBinding".equals(method.getName()) && args != null && args.length == 1 && "foo".equals(args[0])) {
return rc;
} else if ("getEntityBindings".equals(method.getName())) {
return entityBindings;
}
return method.invoke(metadata, args);
}
});
SessionFactoryBuilder sfb = new SessionFactoryBuilderImpl(wrapper);
SessionFactoryImpl sfi = (SessionFactoryImpl) sfb.build();
Map<String, Filter> filters = Collections.emptyMap();
HQLQueryPlan hqlQueryPlan = new HQLQueryPlan("from foo", false, filters, sfi);
IHQLQueryPlan facade = facadeFactory.createHQLQueryPlan(hqlQueryPlan);
assertSame(hqlQueryPlan, ((IFacade) facade).getTarget());
}
Aggregations