List of usage examples for org.hibernate.type ManyToOneType getAssociatedEntityName
public final String getAssociatedEntityName()
From source file:com.autobizlogic.abl.metadata.hibernate.HibMetaRole.java
License:Open Source License
/** * Get the inverse of a many-to-one role * @param entityName The entity owning the role * @param rName The role name/*from w ww.j a v a 2 s . com*/ * @return Null if no inverse was found, otherwise [entity name, role name] of the inverse role */ private String[] getInverseOfSingleRole(String entityName, String rName) { String[] result = new String[2]; SessionFactory sessFact = ((HibMetaModel) getMetaEntity().getMetaModel()).getSessionFactory(); AbstractEntityPersister childMeta = (AbstractEntityPersister) sessFact.getClassMetadata(entityName); int propIdx = childMeta.getPropertyIndex(rName); String[] cnames = childMeta.getPropertyColumnNames(propIdx); Type parentType = childMeta.getPropertyType(rName); if (parentType instanceof OneToOneType) return getInverseOfOneToOneRole(entityName, rName); if (!(parentType instanceof ManyToOneType)) throw new RuntimeException("Inverse of single-valued role " + entityName + "." + rName + " is neither single-valued not multi-valued"); ManyToOneType manyType = (ManyToOneType) parentType; String parentEntityName = manyType.getAssociatedEntityName(); AbstractEntityPersister parentMeta = (AbstractEntityPersister) sessFact.getClassMetadata(parentEntityName); String[] propNames = parentMeta.getPropertyNames(); for (int i = 0; i < propNames.length; i++) { Type type = parentMeta.getPropertyType(propNames[i]); if (!type.isCollectionType()) continue; CollectionType collType = (CollectionType) type; if (!collType.getAssociatedEntityName((SessionFactoryImplementor) sessFact).equals(entityName)) continue; AbstractCollectionPersister persister = (AbstractCollectionPersister) sessFact .getCollectionMetadata(parentEntityName + "." + propNames[i]); String[] colNames = persister.getKeyColumnNames(); if (cnames.length != colNames.length) continue; boolean columnMatch = true; for (int j = 0; j < cnames.length; j++) { if (!cnames[j].equals(colNames[j])) { columnMatch = false; break; } } if (columnMatch) { result[0] = parentEntityName; result[1] = propNames[i]; return result; } } return null; }
From source file:com.blazebit.persistence.integration.hibernate.base.HibernateExtendedQuerySupport.java
License:Apache License
@Override public int getSqlSelectAttributePosition(EntityManager em, Query query, String expression) { if (expression.contains(".")) { // TODO: implement throw new UnsupportedOperationException("Embeddables are not yet supported!"); }//from w w w . ja v a 2s .co m SessionImplementor session = em.unwrap(SessionImplementor.class); HQLQueryPlan plan = getOriginalQueryPlan(session, query); if (plan.getTranslators().length > 1) { throw new IllegalArgumentException("No support for multiple translators yet!"); } QueryTranslator translator = plan.getTranslators()[0]; try { QueryNode queryNode = getField(translator, "sqlAst"); SelectClause selectClause = queryNode.getSelectClause(); Type[] queryReturnTypes = selectClause.getQueryReturnTypes(); boolean found = false; // The ordinal is 1 based int position = 1; for (int i = 0; i < queryReturnTypes.length; i++) { Type t = queryReturnTypes[i]; if (t instanceof ManyToOneType) { ManyToOneType manyToOneType = (ManyToOneType) t; AbstractEntityPersister persister = (AbstractEntityPersister) session.getFactory() .getEntityPersister(manyToOneType.getAssociatedEntityName()); int propertyIndex = persister.getPropertyIndex(expression); found = true; for (int j = 0; j < propertyIndex; j++) { position += persister.getPropertyColumnNames(j).length; } // Increment to the actual property position position++; } else { position++; } } if (found) { return position; } AST selectItem = selectClause.getFirstChild(); while (selectItem != null && (selectItem.getType() == SqlTokenTypes.DISTINCT || selectItem.getType() == SqlTokenTypes.ALL)) { selectItem = selectItem.getNextSibling(); } position = 1; for (AST n = selectItem; n != null; n = n.getNextSibling()) { if (n instanceof DotNode) { DotNode dot = (DotNode) n; if (expression.equals(dot.getPropertyPath())) { // Check if the property is an embeddable if (dot.getText().contains(",")) { throw new IllegalStateException("Can't order by the embeddable: " + expression); } found = true; break; } } position++; } if (found) { return position; } return -1; } catch (Exception e1) { throw new RuntimeException(e1); } }
From source file:it.eng.qbe.datasource.hibernate.HibernatePersistenceManager.java
License:Mozilla Public License
public void updateRecord(JSONObject aRecord, RegistryConfiguration registryConf) { SessionFactory sf = dataSource.getHibernateSessionFactory(); Configuration cfg = dataSource.getHibernateConfiguration(); Session aSession = null;// w w w . ja v a 2 s . co m Transaction tx = null; try { aSession = sf.openSession(); tx = aSession.beginTransaction(); String entityName = registryConf.getEntity(); PersistentClass classMapping = cfg.getClassMapping(entityName); ClassMetadata classMetadata = sf.getClassMetadata(entityName); String keyName = classMetadata.getIdentifierPropertyName(); Object key = aRecord.get(keyName); Property propertyId = classMapping.getProperty(keyName); //casts the id to the appropriate java type Object keyConverted = this.convertValue(key, propertyId); Object obj = aSession.load(entityName, (Serializable) keyConverted); Iterator it = aRecord.keys(); while (it.hasNext()) { String aKey = (String) it.next(); if (keyName.equals(aKey)) { continue; } Column c = registryConf.getColumnConfiguration(aKey); if (c.getSubEntity() != null) { // case of foreign key Property property = classMapping.getProperty(c.getSubEntity()); Type propertyType = property.getType(); if (propertyType instanceof ManyToOneType) { ManyToOneType manyToOnePropertyType = (ManyToOneType) propertyType; String entityType = manyToOnePropertyType.getAssociatedEntityName(); Object referenced = getReferencedObject(aSession, entityType, c.getField(), aRecord.get(aKey)); Setter setter = property.getSetter(obj.getClass()); setter.getMethod().invoke(obj, referenced); } else { throw new SpagoBIRuntimeException( "Property " + c.getSubEntity() + " is not a many-to-one relation"); } } else { // case of property Property property = classMapping.getProperty(aKey); Setter setter = property.getSetter(obj.getClass()); Object valueObj = aRecord.get(aKey); if (valueObj != null && !valueObj.equals("")) { Object valueConverted = this.convertValue(valueObj, property); setter.getMethod().invoke(obj, valueConverted); } } } aSession.saveOrUpdate(obj); tx.commit(); } catch (Exception e) { if (tx != null) { tx.rollback(); } throw new RuntimeException(e); } finally { if (aSession != null) { if (aSession.isOpen()) aSession.close(); } } }
From source file:it.eng.qbe.model.structure.builder.hibernate.HibernateModelStructureBuilder.java
License:Mozilla Public License
public List addNormalFields(IModelEntity dataMartEntity) { ClassMetadata classMetadata;/* ww w.j a v a 2 s .com*/ PersistentClass classMapping; String[] propertyNames; Property property; Type propertyType; classMetadata = getDataSource().getHibernateSessionFactory().getClassMetadata(dataMartEntity.getType()); classMapping = getDataSource().getHibernateConfiguration().getClassMapping(dataMartEntity.getType()); propertyNames = classMetadata.getPropertyNames(); List subEntities = new ArrayList(); String propertyName = null; for (int i = 0; i < propertyNames.length; i++) { property = classMapping.getProperty(propertyNames[i]); // TEST if they are the same: if so use the first invocation propertyType = property.getType(); Iterator columnIterator = property.getColumnIterator(); Column column; if (propertyType instanceof ManyToOneType) { // chiave esterna ManyToOneType manyToOnePropertyType = (ManyToOneType) propertyType; String entityType = manyToOnePropertyType.getAssociatedEntityName(); String columnName = null; if (columnIterator.hasNext()) { column = (Column) columnIterator.next(); columnName = column.getName(); // ???? } propertyName = propertyNames[i]; //String entityName = getEntityNameFromEntityType(entityType); String entityName = propertyName; IModelEntity subentity = new ModelEntity(entityName, null, columnName, entityType, dataMartEntity.getStructure()); subEntities.add(subentity); } else if (propertyType instanceof CollectionType) { // chiave interna } else { // normal field propertyName = propertyNames[i]; String type = propertyType.getName(); int scale = 0; int precision = 0; if (columnIterator.hasNext()) { column = (Column) columnIterator.next(); scale = column.getScale(); precision = column.getPrecision(); } IModelField datamartField = dataMartEntity.addNormalField(propertyName); datamartField.setType(type); datamartField.setPrecision(precision); datamartField.setLength(scale); propertiesInitializer.addProperties(datamartField); } } return subEntities; }