Java Code Examples for org.hibernate.type.ComponentType

The following examples show how to use org.hibernate.type.ComponentType. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: lams   Source File: AbstractEntityTuplizer.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Extract a component property value.
 *
 * @param type The component property types.
 * @param component The component instance itself.
 * @param propertyPath The property path for the property to be extracted.
 *
 * @return The property value extracted.
 */
protected Object getComponentValue(ComponentType type, Object component, String propertyPath) {
	final int loc = propertyPath.indexOf( '.' );
	final String basePropertyName = loc > 0
			? propertyPath.substring( 0, loc )
			: propertyPath;
	final int index = findSubPropertyIndex( type, basePropertyName );
	final Object baseValue = type.getPropertyValue( component, index );
	if ( loc > 0 ) {
		if ( baseValue == null ) {
			return null;
		}
		return getComponentValue(
				(ComponentType) type.getSubtypes()[index],
				baseValue,
				propertyPath.substring( loc + 1 )
		);
	}
	else {
		return baseValue;
	}

}
 
Example 2
Source Project: ysoserial-modified   Source File: Hibernate1.java    License: MIT License 6 votes vote down vote up
static Object makeCaller ( Object tpl, Object getters ) throws NoSuchMethodException, InstantiationException, IllegalAccessException,
        InvocationTargetException, NoSuchFieldException, Exception, ClassNotFoundException {
    PojoComponentTuplizer tup = Reflections.createWithoutConstructor(PojoComponentTuplizer.class);
    Reflections.getField(AbstractComponentTuplizer.class, "getters").set(tup, getters);

    ComponentType t = Reflections.createWithConstructor(ComponentType.class, AbstractType.class, new Class[0], new Object[0]);
    Reflections.setFieldValue(t, "componentTuplizer", tup);
    Reflections.setFieldValue(t, "propertySpan", 1);
    Reflections.setFieldValue(t, "propertyTypes", new Type[] {
        t
    });

    TypedValue v1 = new TypedValue(t, null);
    Reflections.setFieldValue(v1, "value", tpl);
    Reflections.setFieldValue(v1, "type", t);

    TypedValue v2 = new TypedValue(t, null);
    Reflections.setFieldValue(v2, "value", tpl);
    Reflections.setFieldValue(v2, "type", t);

    return Gadgets.makeMap(v1, v2);
}
 
Example 3
public Object getPropertyValue(Object entity, String propertyPath) throws HibernateException {
	
	int loc = propertyPath.indexOf('.');
	String basePropertyName = loc>0 ?
		propertyPath.substring(0, loc) : propertyPath;
		
	int index = entityMetamodel.getPropertyIndex( basePropertyName );
	Object baseValue = getPropertyValue( entity, index );
	if ( loc>0 ) {
		ComponentType type = (ComponentType) entityMetamodel.getPropertyTypes()[index];
		return getComponentValue( type, baseValue, propertyPath.substring(loc+1) );
	}
	else {
		return baseValue;
	}
}
 
Example 4
public void testComponentQueries() {
	Session s = openSession();
	s.beginTransaction();

	Type[] types = s.createQuery( "select h.name from Human h" ).getReturnTypes();
	assertEquals( 1, types.length );
	assertTrue( types[0] instanceof ComponentType );

	// Test the ability to perform comparisions between component values
	s.createQuery( "from Human h where h.name = h.name" ).list();
	s.createQuery( "from Human h where h.name = :name" ).setParameter( "name", new Name() ).list();
	s.createQuery( "from Human where name = :name" ).setParameter( "name", new Name() ).list();
	s.createQuery( "from Human h where :name = h.name" ).setParameter( "name", new Name() ).list();
	s.createQuery( "from Human h where :name <> h.name" ).setParameter( "name", new Name() ).list();

	// Test the ability to perform comparisions between a component and an explicit row-value
	s.createQuery( "from Human h where h.name = ('John', 'X', 'Doe')" ).list();
	s.createQuery( "from Human h where ('John', 'X', 'Doe') = h.name" ).list();
	s.createQuery( "from Human h where ('John', 'X', 'Doe') <> h.name" ).list();
	s.createQuery( "from Human h where ('John', 'X', 'Doe') >= h.name" ).list();

	s.createQuery( "from Human h order by h.name" ).list();

	s.getTransaction().commit();
	s.close();
}
 
Example 5
Source Project: ysoserial   Source File: Hibernate1.java    License: MIT License 6 votes vote down vote up
static Object makeHibernate45Caller ( Object tpl, Object getters ) throws NoSuchMethodException, InstantiationException, IllegalAccessException,
        InvocationTargetException, NoSuchFieldException, Exception, ClassNotFoundException {
    PojoComponentTuplizer tup = Reflections.createWithoutConstructor(PojoComponentTuplizer.class);
    Reflections.getField(AbstractComponentTuplizer.class, "getters").set(tup, getters);

    ComponentType t = Reflections.createWithConstructor(ComponentType.class, AbstractType.class, new Class[0], new Object[0]);
    Reflections.setFieldValue(t, "componentTuplizer", tup);
    Reflections.setFieldValue(t, "propertySpan", 1);
    Reflections.setFieldValue(t, "propertyTypes", new Type[] {
        t
    });

    TypedValue v1 = new TypedValue(t, null);
    Reflections.setFieldValue(v1, "value", tpl);
    Reflections.setFieldValue(v1, "type", t);

    TypedValue v2 = new TypedValue(t, null);
    Reflections.setFieldValue(v2, "value", tpl);
    Reflections.setFieldValue(v2, "type", t);

    return Gadgets.makeMap(v1, v2);
}
 
Example 6
Source Project: lams   Source File: AbstractEntityPersister.java    License: GNU General Public License v2.0 5 votes vote down vote up
private boolean isValueGenerationRequired(NonIdentifierAttribute attribute, GenerationTiming matchTiming) {
	if ( attribute.getType() instanceof ComponentType ) {
		final ComponentType type = (ComponentType) attribute.getType();
		final ValueGeneration[] propertyValueGenerationStrategies = type.getPropertyValueGenerationStrategies();
		for ( int i = 0; i < propertyValueGenerationStrategies.length; i++ ) {
			if ( isReadRequired( propertyValueGenerationStrategies[i], matchTiming ) ) {
				return true;
			}
		}
		return false;
	}
	else {
		return isReadRequired( attribute.getValueGenerationStrategy(), matchTiming );
	}
}
 
Example 7
Source Project: lams   Source File: AbstractEntityTuplizer.java    License: GNU General Public License v2.0 5 votes vote down vote up
private static MappedIdentifierValueMarshaller buildMappedIdentifierValueMarshaller(
		String entityName,
		SessionFactoryImplementor sessionFactory,
		ComponentType mappedIdClassComponentType,
		ComponentType virtualIdComponent) {
	// so basically at this point we know we have a "mapped" composite identifier
	// which is an awful way to say that the identifier is represented differently
	// in the entity and in the identifier value.  The incoming value should
	// be an instance of the mapped identifier class (@IdClass) while the incoming entity
	// should be an instance of the entity class as defined by metamodel.
	//
	// However, even within that we have 2 potential scenarios:
	//		1) @IdClass types and entity @Id property types match
	//			- return a NormalMappedIdentifierValueMarshaller
	//		2) They do not match
	//			- return a IncrediblySillyJpaMapsIdMappedIdentifierValueMarshaller
	boolean wereAllEquivalent = true;
	// the sizes being off is a much bigger problem that should have been caught already...
	for ( int i = 0; i < virtualIdComponent.getSubtypes().length; i++ ) {
		if ( virtualIdComponent.getSubtypes()[i].isEntityType()
				&& !mappedIdClassComponentType.getSubtypes()[i].isEntityType() ) {
			wereAllEquivalent = false;
			break;
		}
	}

	return wereAllEquivalent ?
			new NormalMappedIdentifierValueMarshaller( virtualIdComponent, mappedIdClassComponentType ) :
			new IncrediblySillyJpaMapsIdMappedIdentifierValueMarshaller(
					entityName,
					sessionFactory,
					virtualIdComponent,
					mappedIdClassComponentType
	);
}
 
Example 8
Source Project: lams   Source File: AbstractEntityTuplizer.java    License: GNU General Public License v2.0 5 votes vote down vote up
private IncrediblySillyJpaMapsIdMappedIdentifierValueMarshaller(
		String entityName,
		SessionFactoryImplementor sessionFactory,
		ComponentType virtualIdComponent,
		ComponentType mappedIdentifierType) {
	this.sessionFactory = sessionFactory;
	this.entityName = entityName;
	this.virtualIdComponent = virtualIdComponent;
	this.mappedIdentifierType = mappedIdentifierType;
}
 
Example 9
Source Project: lams   Source File: AbstractEntityTuplizer.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public Object getPropertyValue(Object entity, String propertyPath) throws HibernateException {
	int loc = propertyPath.indexOf( '.' );
	String basePropertyName = loc > 0
			? propertyPath.substring( 0, loc )
			: propertyPath;
	//final int index = entityMetamodel.getPropertyIndexOrNull( basePropertyName );
	Integer index = entityMetamodel.getPropertyIndexOrNull( basePropertyName );
	if ( index == null ) {
		propertyPath = PropertyPath.IDENTIFIER_MAPPER_PROPERTY + "." + propertyPath;
		loc = propertyPath.indexOf( '.' );
		basePropertyName = loc > 0
				? propertyPath.substring( 0, loc )
				: propertyPath;
	}
	index = entityMetamodel.getPropertyIndexOrNull( basePropertyName );
	final Object baseValue = getPropertyValue( entity, index );
	if ( loc > 0 ) {
		if ( baseValue == null ) {
			return null;
		}
		return getComponentValue(
				(ComponentType) entityMetamodel.getPropertyTypes()[index],
				baseValue,
				propertyPath.substring( loc + 1 )
		);
	}
	else {
		return baseValue;
	}
}
 
Example 10
Source Project: lams   Source File: AbstractEntityTuplizer.java    License: GNU General Public License v2.0 5 votes vote down vote up
private int findSubPropertyIndex(ComponentType type, String subPropertyName) {
	final String[] propertyNames = type.getPropertyNames();
	for ( int index = 0; index < propertyNames.length; index++ ) {
		if ( subPropertyName.equals( propertyNames[index] ) ) {
			return index;
		}
	}
	throw new MappingException( "component property not found: " + subPropertyName );
}
 
Example 11
Source Project: lams   Source File: QueryParameters.java    License: GNU General Public License v2.0 5 votes vote down vote up
private int getNumberOfParametersCoveredBy(Type[] subtypes) {
	int numberOfParameters = 0;
	for ( Type type : subtypes ) {
		if ( type.isComponentType() ) {
			numberOfParameters = numberOfParameters + getNumberOfParametersCoveredBy( ((ComponentType) type).getSubtypes() );
		}
		else {
			numberOfParameters++;
		}
	}
	return numberOfParameters;
}
 
Example 12
private String fieldType(Column currentColumn) {
	Value value = currentColumn.getValue();
	Type type = value.getType();
	while ( type.isEntityType() || type.isComponentType() ) {
		if ( type.isEntityType() ) {
			type = ( (SimpleValue) value ).getMetadata().getIdentifierType( type.getName() );
		}
		if ( type.isComponentType() ) {
			int i = 0;
			boolean columnFound = false;
			// search which nested property is mapped to the given column
			for ( Iterator<Selectable> ci = value.getColumnIterator(); ci.hasNext(); ++i ) {
				if ( currentColumn.getName().equals( ci.next().getText() ) ) {
					type = ( (ComponentType) type ).getSubtypes()[i];
					columnFound = true;
					break;
				}
			}
			if ( !columnFound ) {
				throw new IllegalArgumentException( "Cannot determine type for column " + currentColumn );
			}
		}
	}
	GridType gridType = serviceRegistry.getService( TypeTranslator.class ).getType( type );
	if ( gridType instanceof EnumType ) {
		return enumFieldType( (EnumType) gridType );
	}
	if ( gridType instanceof YesNoType ) {
		return STRING_CLASS_NAME;
	}
	if ( gridType instanceof NumericBooleanType ) {
		return INTEGER_CLASS_NAME;
	}
	Class<?> returnedClass = type.getReturnedClass();
	if ( Character.class.equals( returnedClass ) ) {
		return STRING_CLASS_NAME;
	}
	return returnedClass.getName();
}
 
Example 13
private Type buildType() {
	// TODO : temporary initial step towards HHH-1907
	ComponentMetamodel metamodel = new ComponentMetamodel( this );
	if ( isEmbedded() ) {
		return new EmbeddedComponentType( metamodel );
	}
	else {
		return new ComponentType( metamodel );
	}
}
 
Example 14
public Serializable getIdentifier(Object entity) throws HibernateException {
	final Object id;
	if ( entityMetamodel.getIdentifierProperty().isEmbedded() ) {
		id = entity;
	}
	else {
		if ( idGetter == null ) {
			if (identifierMapperType==null) {
				throw new HibernateException( "The class has no identifier property: " + getEntityName() );
			}
			else {
				ComponentType copier = (ComponentType) entityMetamodel.getIdentifierProperty().getType();
				id = copier.instantiate( getEntityMode() );
				copier.setPropertyValues( id, identifierMapperType.getPropertyValues( entity, getEntityMode() ), getEntityMode() );
			}
		}
		else {
			id = idGetter.get( entity );
		}
	}

	try {
		return (Serializable) id;
	}
	catch ( ClassCastException cce ) {
		StringBuffer msg = new StringBuffer( "Identifier classes must be serializable. " );
		if ( id != null ) {
			msg.append( id.getClass().getName() + " is not serializable. " );
		}
		if ( cce.getMessage() != null ) {
			msg.append( cce.getMessage() );
		}
		throw new ClassCastException( msg.toString() );
	}
}
 
Example 15
/**
 * Extract a component property value.
 *
 * @param type The component property types.
 * @param component The component instance itself.
 * @param propertyPath The property path for the property to be extracted.
 * @return The property value extracted.
 */
protected Object getComponentValue(ComponentType type, Object component, String propertyPath) {
	
	int loc = propertyPath.indexOf('.');
	String basePropertyName = loc>0 ?
		propertyPath.substring(0, loc) : propertyPath;
	
	String[] propertyNames = type.getPropertyNames();
	int index=0;
	for ( ; index<propertyNames.length; index++ ) {
		if ( basePropertyName.equals( propertyNames[index] ) ) break;
	}
	if (index==propertyNames.length) {
		throw new MappingException( "component property not found: " + basePropertyName );
	}
	
	Object baseValue = type.getPropertyValue( component, index, getEntityMode() );
	
	if ( loc>0 ) {
		ComponentType subtype = (ComponentType) type.getSubtypes()[index];
		return getComponentValue( subtype, baseValue, propertyPath.substring(loc+1) );
	}
	else {
		return baseValue;
	}
	
}
 
Example 16
Source Project: ysoserial   Source File: Hibernate1.java    License: MIT License 5 votes vote down vote up
static Object makeHibernate3Caller ( Object tpl, Object getters ) throws NoSuchMethodException, InstantiationException, IllegalAccessException,
        InvocationTargetException, NoSuchFieldException, Exception, ClassNotFoundException {
    // Load at runtime to avoid dependency conflicts
    Class entityEntityModeToTuplizerMappingClass = Class.forName("org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping");
    Class entityModeToTuplizerMappingClass = Class.forName("org.hibernate.tuple.EntityModeToTuplizerMapping");
    Class typedValueClass = Class.forName("org.hibernate.engine.TypedValue");

    PojoComponentTuplizer tup = Reflections.createWithoutConstructor(PojoComponentTuplizer.class);
    Reflections.getField(AbstractComponentTuplizer.class, "getters").set(tup, getters);
    Reflections.getField(AbstractComponentTuplizer.class, "propertySpan").set(tup, 1);

    ComponentType t = Reflections.createWithConstructor(ComponentType.class, AbstractType.class, new Class[0], new Object[0]);
    HashMap hm = new HashMap();
    hm.put(EntityMode.POJO, tup);
    Object emtm = Reflections.createWithConstructor(entityEntityModeToTuplizerMappingClass, entityModeToTuplizerMappingClass, new Class[]{ Map.class }, new Object[]{ hm });
    Reflections.setFieldValue(t, "tuplizerMapping", emtm);
    Reflections.setFieldValue(t, "propertySpan", 1);
    Reflections.setFieldValue(t, "propertyTypes", new Type[] {
        t
    });

    Constructor<?> typedValueConstructor = typedValueClass.getDeclaredConstructor(Type.class, Object.class, EntityMode.class);
    Object v1 = typedValueConstructor.newInstance(t, null, EntityMode.POJO);
    Reflections.setFieldValue(v1, "value", tpl);
    Reflections.setFieldValue(v1, "type", t);

    Object v2 = typedValueConstructor.newInstance(t, null, EntityMode.POJO);
    Reflections.setFieldValue(v2, "value", tpl);
    Reflections.setFieldValue(v2, "type", t);

    return Gadgets.makeMap(v1, v2);
}
 
Example 17
Source Project: lams   Source File: AttributeFactory.java    License: GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private <Y> Type<Y> getMetaModelType(ValueContext typeContext) {
	switch ( typeContext.getValueClassification() ) {
		case BASIC: {
			return new BasicTypeImpl<Y>(
					typeContext.getBindableType(),
					Type.PersistenceType.BASIC
			);
		}
		case ENTITY: {
			final org.hibernate.type.EntityType type = (EntityType) typeContext.getValue().getType();
			return (Type<Y>) context.locateEntityType( type.getAssociatedEntityName() );
		}
		case EMBEDDABLE: {
			final Component component = (Component) typeContext.getValue();
			Class javaType;
			if ( component.getComponentClassName() == null ) {
				javaType = typeContext.getBindableType();
			}
			else {
				javaType = component.getComponentClass();
			}
			final EmbeddableTypeImpl<Y> embeddableType = new EmbeddableTypeImpl<Y>(
					javaType,
					typeContext.getAttributeMetadata().getOwnerType(),
					(ComponentType) typeContext.getValue().getType()
			);
			context.registerEmbeddedableType( embeddableType );
			final Iterator<Property> subProperties = component.getPropertyIterator();
			while ( subProperties.hasNext() ) {
				final Property property = subProperties.next();
				final AttributeImplementor<Y, Object> attribute = buildAttribute( embeddableType, property );
				if ( attribute != null ) {
					embeddableType.getBuilder().addAttribute( attribute );
				}
			}
			embeddableType.lock();
			return embeddableType;
		}
		default: {
			throw new AssertionFailure( "Unknown type : " + typeContext.getValueClassification() );
		}
	}
}
 
Example 18
Source Project: lams   Source File: EmbeddableTypeImpl.java    License: GNU General Public License v2.0 4 votes vote down vote up
public EmbeddableTypeImpl(Class<X> javaType, AbstractManagedType parent, ComponentType hibernateType) {
	super( javaType, null, null );
	this.parent = parent;
	this.hibernateType = hibernateType;
}
 
Example 19
Source Project: lams   Source File: EmbeddableTypeImpl.java    License: GNU General Public License v2.0 4 votes vote down vote up
public ComponentType getHibernateType() {
	return hibernateType;
}
 
Example 20
Source Project: lams   Source File: AbstractEntityTuplizer.java    License: GNU General Public License v2.0 4 votes vote down vote up
private NormalMappedIdentifierValueMarshaller(
		ComponentType virtualIdComponent,
		ComponentType mappedIdentifierType) {
	this.virtualIdComponent = virtualIdComponent;
	this.mappedIdentifierType = mappedIdentifierType;
}