List of usage examples for org.hibernate.mapping Property getPersistentClass
public PersistentClass getPersistentClass()
From source file:com.manydesigns.portofino.persistence.hibernate.HibernateConfig.java
License:Open Source License
private DependantValue createFKSingle(Mappings mappings, PersistentClass clazzOne, PersistentClass clazzMany, Table tableOne, List<Column> oneColumns, List<Column> manyColumns, List<Reference> refs) { DependantValue dv;// w w w . j a v a2 s .c o m Property refProp; Reference reference = refs.get(0); String colFromName = reference.getFromColumn(); String colToName = reference.getToColumn(); String colToPropertyName = reference.getActualToColumn().getActualPropertyName(); refProp = getRefProperty(clazzOne, colToPropertyName); dv = new DependantValue(mappings, clazzMany.getTable(), refProp.getPersistentClass().getKey()); dv.setNullable(true); dv.setUpdateable(true); Iterator it = clazzMany.getTable().getColumnIterator(); while (it.hasNext()) { Column col = (Column) it.next(); if (col.getName().equals(colFromName)) { dv.addColumn(col); manyColumns.add(col); break; } } Iterator it2 = tableOne.getColumnIterator(); while (it2.hasNext()) { Column col = (Column) it2.next(); if (col.getName().equals(colToName)) { oneColumns.add(col); break; } } return dv; }
From source file:org.babyfish.hibernate.cfg.Configuration.java
License:Open Source License
private static void replaceUserCollectionType(Property mappingProperty, Class<? extends org.hibernate.mapping.Collection> hibernateCollectionType, Class<? extends AbstractMACollectionType> babyfishCollectionType) { /*/* w w w . java 2s.c o m*/ * Don't invoke property.getType() or property.getValue().getType() * that will cause the creating of original collection-type before the replacement. * that is is slow */ Value value = mappingProperty.getValue(); if (!(value instanceof org.hibernate.mapping.Collection)) { throw new MappingException('"' + mappingProperty.getPersistentClass().getEntityName() + '.' + mappingProperty.getName() + "\" must be mapped as collection."); } org.hibernate.mapping.Collection collection = (org.hibernate.mapping.Collection) value; String typeName = collection.getTypeName(); if (typeName == null) { if (!hibernateCollectionType.isAssignableFrom(value.getClass())) { throw new MappingException('"' + mappingProperty.getPersistentClass().getEntityName() + '.' + mappingProperty.getName() + "\" must be mapped collection whose hibernate type is \"" + hibernateCollectionType.getName() + "\"."); } collection.setTypeName(babyfishCollectionType.getName()); } else { Class<?> userCollctionType; try { userCollctionType = ReflectHelper.classForName(typeName); } catch (ClassNotFoundException ex) { throw new MappingException( '"' + mappingProperty.getPersistentClass().getEntityName() + '.' + mappingProperty.getName() + "\" must be mapped as collection whose attribute \"collection-type\" is \"" + typeName + "\", but the there is no java type names\"" + typeName + "\"."); } if (!babyfishCollectionType.isAssignableFrom(userCollctionType)) { throw new MappingException( '"' + mappingProperty.getPersistentClass().getEntityName() + '.' + mappingProperty.getName() + "\" must be mapped as collection whose attribut \"collection-type\" is \"" + typeName + "\", but the there class \"" + typeName + "\" is not \"" + babyfishCollectionType.getName() + "\" or its derived class."); } } }
From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java
License:Apache License
@SuppressWarnings("unchecked") protected String buildOrderByClause(String hqlOrderBy, PersistentClass associatedClass, String role, String defaultOrder) {/* w w w.j ava2 s .c om*/ String orderByString = null; if (hqlOrderBy != null) { List<String> properties = new ArrayList<String>(); List<String> ordering = new ArrayList<String>(); StringBuilder orderByBuffer = new StringBuilder(); if (hqlOrderBy.length() == 0) { //order by id Iterator<?> it = associatedClass.getIdentifier().getColumnIterator(); while (it.hasNext()) { Selectable col = (Selectable) it.next(); orderByBuffer.append(col.getText()).append(" asc").append(", "); } } else { StringTokenizer st = new StringTokenizer(hqlOrderBy, " ,", false); String currentOrdering = defaultOrder; //FIXME make this code decent while (st.hasMoreTokens()) { String token = st.nextToken(); if (isNonPropertyToken(token)) { if (currentOrdering != null) { throw new GrailsDomainException( "Error while parsing sort clause: " + hqlOrderBy + " (" + role + ")"); } currentOrdering = token; } else { //Add ordering of the previous if (currentOrdering == null) { //default ordering ordering.add("asc"); } else { ordering.add(currentOrdering); currentOrdering = null; } properties.add(token); } } ordering.remove(0); //first one is the algorithm starter // add last one ordering if (currentOrdering == null) { //default ordering ordering.add(defaultOrder); } else { ordering.add(currentOrdering); currentOrdering = null; } int index = 0; for (String property : properties) { Property p = BinderHelper.findPropertyByName(associatedClass, property); if (p == null) { throw new GrailsDomainException("property from sort clause not found: " + associatedClass.getEntityName() + "." + property); } PersistentClass pc = p.getPersistentClass(); String table; if (pc == null) { table = ""; } else if (pc == associatedClass || (associatedClass instanceof SingleTableSubclass && pc.getMappedClass().isAssignableFrom(associatedClass.getMappedClass()))) { table = ""; } else { table = pc.getTable().getQuotedName() + "."; } Iterator<?> propertyColumns = p.getColumnIterator(); while (propertyColumns.hasNext()) { Selectable column = (Selectable) propertyColumns.next(); orderByBuffer.append(table).append(column.getText()).append(" ").append(ordering.get(index)) .append(", "); } index++; } } orderByString = orderByBuffer.substring(0, orderByBuffer.length() - 2); } return orderByString; }
From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder.java
License:Apache License
@SuppressWarnings("unchecked") private static String buildOrderByClause(String hqlOrderBy, PersistentClass associatedClass, String role, String defaultOrder) {/* w w w . j a v a2 s. co m*/ String orderByString = null; if (hqlOrderBy != null) { List<String> properties = new ArrayList<String>(); List<String> ordering = new ArrayList<String>(); StringBuilder orderByBuffer = new StringBuilder(); if (hqlOrderBy.length() == 0) { //order by id Iterator<?> it = associatedClass.getIdentifier().getColumnIterator(); while (it.hasNext()) { Selectable col = (Selectable) it.next(); orderByBuffer.append(col.getText()).append(" asc").append(", "); } } else { StringTokenizer st = new StringTokenizer(hqlOrderBy, " ,", false); String currentOrdering = defaultOrder; //FIXME make this code decent while (st.hasMoreTokens()) { String token = st.nextToken(); if (isNonPropertyToken(token)) { if (currentOrdering != null) { throw new GrailsDomainException( "Error while parsing sort clause: " + hqlOrderBy + " (" + role + ")"); } currentOrdering = token; } else { //Add ordering of the previous if (currentOrdering == null) { //default ordering ordering.add("asc"); } else { ordering.add(currentOrdering); currentOrdering = null; } properties.add(token); } } ordering.remove(0); //first one is the algorithm starter // add last one ordering if (currentOrdering == null) { //default ordering ordering.add(defaultOrder); } else { ordering.add(currentOrdering); currentOrdering = null; } int index = 0; for (String property : properties) { Property p = BinderHelper.findPropertyByName(associatedClass, property); if (p == null) { throw new GrailsDomainException("property from sort clause not found: " + associatedClass.getEntityName() + "." + property); } PersistentClass pc = p.getPersistentClass(); String table; if (pc == null) { table = ""; } else if (pc == associatedClass || (associatedClass instanceof SingleTableSubclass && pc.getMappedClass().isAssignableFrom(associatedClass.getMappedClass()))) { table = ""; } else { table = pc.getTable().getQuotedName() + "."; } Iterator<?> propertyColumns = p.getColumnIterator(); while (propertyColumns.hasNext()) { Selectable column = (Selectable) propertyColumns.next(); orderByBuffer.append(table).append(column.getText()).append(" ").append(ordering.get(index)) .append(", "); } index++; } } orderByString = orderByBuffer.substring(0, orderByBuffer.length() - 2); } return orderByString; }
From source file:org.eclipse.emf.teneo.hibernate.HbUtil.java
License:Open Source License
/** Returns the correct accessor on the basis of the type of property */ public static PropertyAccessor getPropertyAccessor(Property mappedProperty, HbDataStore ds, String entityName, EClass mappedEClass) {//from w w w . java 2s. c o m if (mappedProperty.getMetaAttribute(HbConstants.SYNTHETIC_PROPERTY_INDICATOR) != null) { // synthetic return new SyntheticPropertyHandler(mappedProperty.getName()); } else if (mappedProperty.getMetaAttribute(HbMapperConstants.ID_META) != null) { // synthetic // ID return new IdentifierPropertyHandler(); } else if (mappedProperty.getMetaAttribute(HbMapperConstants.VERSION_META) != null) { return ds.getHbContext().createVersionAccessor(); } else if (mappedProperty.getName().compareToIgnoreCase("_identifierMapper") == 0) { // name // is // used // by // hb return new EmbeddedPropertyAccessor(); // new // DummyPropertyHandler(); } else if (mappedProperty.getName().compareToIgnoreCase(HbConstants.PROPERTY_ECONTAINER) == 0) { return ds.getHbContext().createEContainerAccessor(); } else if (mappedProperty.getName() .compareToIgnoreCase(HbConstants.PROPERTY_ECONTAINER_FEATURE_NAME) == 0) { return ds.getExtensionManager().getExtension(NewEContainerFeatureIDPropertyHandler.class); } else if (mappedProperty.getName().compareToIgnoreCase(HbConstants.PROPERTY_ECONTAINER_FEATURE_ID) == 0) { return ds.getHbContext().createEContainerFeatureIDAccessor(); } EClass eClass = null; if (mappedEClass != null) { eClass = mappedEClass; } else { eClass = ds.getEntityNameStrategy().toEClass(entityName); if (eClass == null) { // for components this is the case eClass = ERuntime.INSTANCE.getEClass(entityName); } } final EStructuralFeature efeature = StoreUtil.getEStructuralFeature(eClass, mappedProperty.getName()); if (efeature == null) { throw new HbMapperException("Feature not found for eclass/entity/property " + eClass.getName() + "/" + entityName + "/" + mappedProperty.getName()); } if (log.isDebugEnabled()) { log.debug("Creating property accessor for " + mappedProperty.getName() + "/" + entityName + "/" + efeature.getName()); } // check extra lazy final boolean extraLazy = mappedProperty.getValue() instanceof Collection && ((Collection) mappedProperty.getValue()).isExtraLazy(); if (FeatureMapUtil.isFeatureMap(efeature)) { return ds.getHbContext().createFeatureMapPropertyAccessor(efeature); } else if (efeature instanceof EReference) { final EReference eref = (EReference) efeature; if (eref.isMany()) { return ds.getHbContext().createEListAccessor(efeature, extraLazy, ds.getPersistenceOptions().isMapEMapAsTrueMap()); } else { PropertyAccessor erefPropertyHandler = ds.getHbContext().createEReferenceAccessor(eref); if (erefPropertyHandler instanceof EReferencePropertyHandler) { if (mappedProperty.getPersistentClass() != null) { ((EReferencePropertyHandler) erefPropertyHandler).setId( mappedProperty == mappedProperty.getPersistentClass().getIdentifierProperty()); } } return erefPropertyHandler; } } else { final EAttribute eattr = (EAttribute) efeature; if (eattr.isMany()) { return ds.getHbContext().createEListAccessor(efeature, extraLazy, ds.getPersistenceOptions().isMapEMapAsTrueMap()); } else { // note also array types are going here! final PropertyAccessor pa = ds.getHbContext().createEAttributeAccessor(eattr); // note this check is necessary because maybe somebody override // HBContext.createEAttributeAccessor // to not return a EAttributePropertyHandler if (pa instanceof EAttributePropertyHandler) { final EAttributePropertyHandler eAttributePropertyHandler = (EAttributePropertyHandler) pa; eAttributePropertyHandler.setPersistenceOptions(ds.getPersistenceOptions()); if (mappedProperty.getPersistentClass() != null) { eAttributePropertyHandler.setId( mappedProperty == mappedProperty.getPersistentClass().getIdentifierProperty()); } } return pa; } } }
From source file:org.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java
License:Apache License
@SuppressWarnings("unchecked") protected String buildOrderByClause(String hqlOrderBy, PersistentClass associatedClass, String role, String defaultOrder) {/*from ww w . j ava2s .co m*/ String orderByString = null; if (hqlOrderBy != null) { List<String> properties = new ArrayList<String>(); List<String> ordering = new ArrayList<String>(); StringBuilder orderByBuffer = new StringBuilder(); if (hqlOrderBy.length() == 0) { //order by id Iterator<?> it = associatedClass.getIdentifier().getColumnIterator(); while (it.hasNext()) { Selectable col = (Selectable) it.next(); orderByBuffer.append(col.getText()).append(" asc").append(", "); } } else { StringTokenizer st = new StringTokenizer(hqlOrderBy, " ,", false); String currentOrdering = defaultOrder; //FIXME make this code decent while (st.hasMoreTokens()) { String token = st.nextToken(); if (isNonPropertyToken(token)) { if (currentOrdering != null) { throw new DatastoreConfigurationException( "Error while parsing sort clause: " + hqlOrderBy + " (" + role + ")"); } currentOrdering = token; } else { //Add ordering of the previous if (currentOrdering == null) { //default ordering ordering.add("asc"); } else { ordering.add(currentOrdering); currentOrdering = null; } properties.add(token); } } ordering.remove(0); //first one is the algorithm starter // add last one ordering if (currentOrdering == null) { //default ordering ordering.add(defaultOrder); } else { ordering.add(currentOrdering); currentOrdering = null; } int index = 0; for (String property : properties) { Property p = BinderHelper.findPropertyByName(associatedClass, property); if (p == null) { throw new DatastoreConfigurationException("property from sort clause not found: " + associatedClass.getEntityName() + "." + property); } PersistentClass pc = p.getPersistentClass(); String table; if (pc == null) { table = ""; } else if (pc == associatedClass || (associatedClass instanceof SingleTableSubclass && pc.getMappedClass().isAssignableFrom(associatedClass.getMappedClass()))) { table = ""; } else { table = pc.getTable().getQuotedName() + "."; } Iterator<?> propertyColumns = p.getColumnIterator(); while (propertyColumns.hasNext()) { Selectable column = (Selectable) propertyColumns.next(); orderByBuffer.append(table).append(column.getText()).append(" ").append(ordering.get(index)) .append(", "); } index++; } } orderByString = orderByBuffer.substring(0, orderByBuffer.length() - 2); } return orderByString; }
From source file:org.jboss.tools.hibernate.ui.diagram.editors.model.Utils.java
License:Open Source License
public static String getName(Object obj) { String res = ""; //$NON-NLS-1$ if (obj instanceof PersistentClass) { PersistentClass rootClass = (PersistentClass) obj; if (rootClass.getEntityName() != null) { res = rootClass.getEntityName(); } else {/*www .java2 s . c o m*/ res = rootClass.getClassName(); } } else if (obj instanceof Table) { res = getTableName((Table) obj); } else if (obj instanceof Property) { Property property = (Property) obj; res = getName(property.getPersistentClass()) + "." + property.getName(); //$NON-NLS-1$ } else if (obj instanceof SimpleValue) { SimpleValue sv = (SimpleValue) obj; res = getTableName(sv.getTable()) + "." + sv.getForeignKeyName(); //$NON-NLS-1$ } else if (obj instanceof String) { res = (String) obj; } if (res.length() > 0 && res.indexOf(".") < 0) { //$NON-NLS-1$ return "default." + res; //$NON-NLS-1$ } if (res.length() == 0) { res = "null"; //$NON-NLS-1$ } return res; }
From source file:org.jboss.tools.hibernate3_6.console.EclipseHQLCompletionRequestor.java
License:Open Source License
private Image getImage(HQLCompletionProposal proposal) { String key = null;/* w ww. j a v a 2 s .c o m*/ switch (proposal.getCompletionKind()) { case HQLCompletionProposal.ENTITY_NAME: case HQLCompletionProposal.ALIAS_REF: key = ImageConstants.MAPPEDCLASS; break; case HQLCompletionProposal.PROPERTY: Property property = proposal.getProperty(); if (property != null) { if (property.getPersistentClass() != null && property.getPersistentClass().getIdentifierProperty() == property) { key = ImageConstants.IDPROPERTY; } else { key = getIconNameForValue(property.getValue()); } } else { key = ImageConstants.PROPERTY; } break; case HQLCompletionProposal.KEYWORD: key = null; break; case HQLCompletionProposal.FUNCTION: key = ImageConstants.FUNCTION; break; default: key = null; } return key == null ? null : EclipseImages.getImage(key); }