下面列出了怎么用javax.persistence.metamodel.Type的API类实例代码及写法,或者点击链接到github查看源代码。
@SuppressWarnings({"unchecked"})
public <X, Y> SingularAttributeImpl<X, Y> buildIdAttribute(
AbstractIdentifiableType<X> ownerType,
Property property) {
LOG.trace( "Building identifier attribute [" + ownerType.getTypeName() + "." + property.getName() + "]" );
final AttributeContext<X> attributeContext = wrap( ownerType, property );
final SingularAttributeMetadata<X, Y> attributeMetadata =
(SingularAttributeMetadata<X, Y>) determineAttributeMetadata(
attributeContext,
identifierMemberResolver
);
final Type<Y> metaModelType = getMetaModelType( attributeMetadata.getValueContext() );
return new SingularAttributeImpl.Identifier(
property.getName(),
attributeMetadata.getJavaType(),
ownerType,
attributeMetadata.getMember(),
metaModelType,
attributeMetadata.getPersistentAttributeType()
);
}
@SuppressWarnings({"unchecked"})
public <X, Y> SingularAttributeImpl<X, Y> buildVersionAttribute(
AbstractIdentifiableType<X> ownerType,
Property property) {
LOG.trace( "Building version attribute [ownerType.getTypeName()" + "." + "property.getName()]" );
final AttributeContext<X> attributeContext = wrap( ownerType, property );
final SingularAttributeMetadata<X, Y> attributeMetadata =
(SingularAttributeMetadata<X, Y>) determineAttributeMetadata( attributeContext, versionMemberResolver );
final Type<Y> metaModelType = getMetaModelType( attributeMetadata.getValueContext() );
return new SingularAttributeImpl.Version(
property.getName(),
attributeMetadata.getJavaType(),
ownerType,
attributeMetadata.getMember(),
metaModelType,
attributeMetadata.getPersistentAttributeType()
);
}
private EntityMetamodel getDeclarerEntityMetamodel(AbstractIdentifiableType<?> ownerType) {
final Type.PersistenceType persistenceType = ownerType.getPersistenceType();
if ( persistenceType == Type.PersistenceType.ENTITY ) {
return context.getSessionFactory()
.getMetamodel()
.entityPersister( ownerType.getTypeName() )
.getEntityMetamodel();
}
else if ( persistenceType == Type.PersistenceType.MAPPED_SUPERCLASS ) {
PersistentClass persistentClass =
context.getPersistentClassHostingProperties( (MappedSuperclassTypeImpl<?>) ownerType );
return context.getSessionFactory()
.getMetamodel()
.entityPersister( persistentClass.getClassName() )
.getEntityMetamodel();
}
else {
throw new AssertionFailure( "Cannot get the metamodel for PersistenceType: " + persistenceType );
}
}
public static ParameterizedType getSignatureType(Member member) {
final java.lang.reflect.Type type;
if ( Field.class.isInstance( member ) ) {
type = ( (Field) member ).getGenericType();
}
else if ( Method.class.isInstance( member ) ) {
type = ( (Method) member ).getGenericReturnType();
}
else {
type = ( (MapMember) member ).getType();
}
//this is a raw type
if ( type instanceof Class ) {
return null;
}
return (ParameterizedType) type;
}
public SingularAttributeImpl(
String name,
Class<Y> javaType,
AbstractManagedType<X> declaringType,
Member member,
boolean isIdentifier,
boolean isVersion,
boolean isOptional,
Type<Y> attributeType,
PersistentAttributeType persistentAttributeType) {
super( name, javaType, declaringType, member, persistentAttributeType );
this.isIdentifier = isIdentifier;
this.isVersion = isVersion;
this.isOptional = isOptional;
this.attributeType = attributeType;
}
@Override
@SuppressWarnings("unchecked")
public Type<?> getIdType() {
final SingularAttributeImpl id = locateIdAttribute();
if ( id != null ) {
return id.getType();
}
Set<SingularAttribute<? super X, ?>> idClassAttributes = getIdClassAttributesSafely();
if ( idClassAttributes != null ) {
if ( idClassAttributes.size() == 1 ) {
return idClassAttributes.iterator().next().getType();
}
}
return null;
}
private <Y> JoinImplementor<X, Y> constructJoin(SingularAttribute<? super X, Y> attribute, JoinType jt) {
if ( Type.PersistenceType.BASIC.equals( attribute.getType().getPersistenceType() ) ) {
throw new BasicPathUsageException( "Cannot join to attribute of basic type", attribute );
}
// TODO : runtime check that the attribute in fact belongs to this From's model/bindable
if ( jt.equals( JoinType.RIGHT ) ) {
throw new UnsupportedOperationException( "RIGHT JOIN not supported" );
}
final Class<Y> attributeType = attribute.getBindableJavaType();
return new SingularAttributeJoin<X, Y>(
criteriaBuilder(),
attributeType,
this,
attribute,
jt
);
}
public MapKeyAttribute(CriteriaBuilderImpl criteriaBuilder, MapAttribute<?, K, ?> attribute) {
this.attribute = attribute;
this.jpaType = attribute.getKeyType();
this.jpaBinableJavaType = attribute.getKeyJavaType();
this.jpaBindableType = Type.PersistenceType
.ENTITY.equals( jpaType.getPersistenceType() )
? BindableType.ENTITY_TYPE
: BindableType.SINGULAR_ATTRIBUTE;
String guessedRoleName = determineRole( attribute );
SessionFactoryImplementor sfi = criteriaBuilder.getEntityManagerFactory().getSessionFactory();
mapPersister = sfi.getCollectionPersister( guessedRoleName );
if ( mapPersister == null ) {
throw new IllegalStateException( "Could not locate collection persister [" + guessedRoleName + "]" );
}
mapKeyType = mapPersister.getIndexType();
if ( mapKeyType == null ) {
throw new IllegalStateException( "Could not determine map-key type [" + guessedRoleName + "]" );
}
this.persistentAttributeType = mapKeyType.isEntityType()
? PersistentAttributeType.MANY_TO_ONE
: mapKeyType.isComponentType()
? PersistentAttributeType.EMBEDDED
: PersistentAttributeType.BASIC;
}
@Override
public Type<java.lang.String> getElementType() {
return new Type<java.lang.String>() {
@Override
public Class<java.lang.String> getJavaType() {
return java.lang.String.class;
}
@Override
public javax.persistence.metamodel.Type.PersistenceType getPersistenceType() {
return null;
}
};
}
@SuppressWarnings({"unchecked"})
public <X, Y> AttributeImplementor<X, Y> buildAttribute(AbstractManagedType<X> ownerType, Property property) {
if ( property.isSynthetic() ) {
// hide synthetic/virtual properties (fabricated by Hibernate) from the JPA metamodel.
LOG.tracef( "Skipping synthetic property %s(%s)", ownerType.getTypeName(), property.getName() );
return null;
}
LOG.trace( "Building attribute [" + ownerType.getTypeName() + "." + property.getName() + "]" );
final AttributeContext<X> attributeContext = wrap( ownerType, property );
final AttributeMetadata<X, Y> attributeMetadata =
determineAttributeMetadata( attributeContext, normalMemberResolver );
if ( attributeMetadata == null ) {
return null;
}
if ( attributeMetadata.isPlural() ) {
return buildPluralAttribute( (PluralAttributeMetadata) attributeMetadata );
}
final SingularAttributeMetadata<X, Y> singularAttributeMetadata = (SingularAttributeMetadata<X, Y>) attributeMetadata;
final Type<Y> metaModelType = getMetaModelType( singularAttributeMetadata.getValueContext() );
return new SingularAttributeImpl<X, Y>(
attributeMetadata.getName(),
attributeMetadata.getJavaType(),
ownerType,
attributeMetadata.getMember(),
false,
false,
property.isOptional(),
metaModelType,
attributeMetadata.getPersistentAttributeType()
);
}
@SuppressWarnings("unchecked")
private <X, Y, E, K> AttributeImplementor<X, Y> buildPluralAttribute(PluralAttributeMetadata<X, Y, E> attributeMetadata) {
final Type<E> elementType = getMetaModelType( attributeMetadata.getElementValueContext() );
if ( java.util.Map.class.isAssignableFrom( attributeMetadata.getJavaType() ) ) {
final Type<K> keyType = getMetaModelType( attributeMetadata.getMapKeyValueContext() );
return PluralAttributeImpl.create(
attributeMetadata.getOwnerType(),
elementType,
attributeMetadata.getJavaType(),
keyType
)
.member( attributeMetadata.getMember() )
.property( attributeMetadata.getPropertyMapping() )
.persistentAttributeType( attributeMetadata.getPersistentAttributeType() )
.build();
}
return PluralAttributeImpl.create(
attributeMetadata.getOwnerType(),
elementType,
attributeMetadata.getJavaType(),
null
)
.member( attributeMetadata.getMember() )
.property( attributeMetadata.getPropertyMapping() )
.persistentAttributeType( attributeMetadata.getPersistentAttributeType() )
.build();
}
public Identifier(
String name,
Class<Y> javaType,
AbstractManagedType<X> declaringType,
Member member,
Type<Y> attributeType,
PersistentAttributeType persistentAttributeType) {
super( name, javaType, declaringType, member, true, false, false, attributeType, persistentAttributeType );
}
public Version(
String name,
Class<Y> javaType,
AbstractManagedType<X> declaringType,
Member member,
Type<Y> attributeType,
PersistentAttributeType persistentAttributeType) {
super( name, javaType, declaringType, member, false, true, false, attributeType, persistentAttributeType );
}
public static <X,C,E,K> Builder<X,C,E,K> create(
AbstractManagedType<X> ownerType,
Type<E> attrType,
Class<C> collectionClass,
Type<K> keyType) {
return new Builder<X,C,E,K>(ownerType, attrType, collectionClass, keyType);
}
@Override
@SuppressWarnings("unchecked")
protected ManagedType<? super X> locateManagedType() {
if ( getModel().getBindableType() == Bindable.BindableType.ENTITY_TYPE ) {
return (ManagedType<? super X>) getModel();
}
else if ( getModel().getBindableType() == Bindable.BindableType.SINGULAR_ATTRIBUTE ) {
final Type joinedAttributeType = ( (SingularAttribute) getAttribute() ).getType();
if ( !ManagedType.class.isInstance( joinedAttributeType ) ) {
throw new UnsupportedOperationException(
"Cannot further dereference attribute join [" + getPathIdentifier() + "] as its type is not a ManagedType"
);
}
return (ManagedType<? super X>) joinedAttributeType;
}
else if ( getModel().getBindableType() == Bindable.BindableType.PLURAL_ATTRIBUTE ) {
final Type elementType = ( (PluralAttribute) getAttribute() ).getElementType();
if ( !ManagedType.class.isInstance( elementType ) ) {
throw new UnsupportedOperationException(
"Cannot further dereference attribute join [" + getPathIdentifier() + "] (plural) as its element type is not a ManagedType"
);
}
return (ManagedType<? super X>) elementType;
}
return super.locateManagedType();
}
@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() );
}
}
}
@Override
public Type<Y> getType() {
return attributeType;
}
private Builder(AbstractManagedType<X> ownerType, Type<E> attrType, Class<C> collectionClass, Type<K> keyType) {
this.type = ownerType;
this.attributeType = attrType;
this.collectionClass = collectionClass;
this.keyType = keyType;
}
@Override
public Type<E> getElementType() {
return elementType;
}
@Override
public Type<K> getKeyType() {
return keyType;
}
@Override
public Type<K> getType() {
return jpaType;
}
public boolean isBasicCollection() {
return Type.PersistenceType.BASIC.equals( getAttribute().getElementType().getPersistenceType() );
}
@Override
public Type<?> getIdType() {
return null;
}
@Override
public javax.persistence.metamodel.Type.PersistenceType getPersistenceType() {
return null;
}
@Override
public Type<T> getType() {
return null;
}
@Override
public Type<String> getElementType() {
// TODO Auto-generated method stub
return null;
}
@Override
public Type<java.lang.String> getElementType() {
return new ElementType();
}
@Override
public javax.persistence.metamodel.Type.PersistenceType getPersistenceType() {
return PersistenceType.BASIC;
}
@Override
public Type<?> getIdType() {
return null;
}
@Override
public javax.persistence.metamodel.Type.PersistenceType getPersistenceType() {
return null;
}