下面列出了怎么用javax.persistence.metamodel.MapAttribute的API类实例代码及写法,或者点击链接到github查看源代码。
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
@SuppressWarnings({ "unchecked" })
public <Y> Path<Y> get(String attributeName) {
if ( ! canBeDereferenced() ) {
throw illegalDereference();
}
final Attribute attribute = locateAttribute( attributeName );
if ( attribute.isCollection() ) {
final PluralAttribute<X,Y,?> pluralAttribute = (PluralAttribute<X,Y,?>) attribute;
if ( PluralAttribute.CollectionType.MAP.equals( pluralAttribute.getCollectionType() ) ) {
return (PluralAttributePath<Y>) this.<Object,Object,Map<Object, Object>>get( (MapAttribute) pluralAttribute );
}
else {
return (PluralAttributePath<Y>) this.get( (PluralAttribute) pluralAttribute );
}
}
else {
return get( (SingularAttribute<X,Y>) attribute );
}
}
@Override
@SuppressWarnings({ "unchecked" })
public MapAttribute<? super X, ?, ?> getMap(String name) {
PluralAttribute<? super X, ?, ?> attribute = getPluralAttribute( name );
if ( attribute == null && getSupertype() != null ) {
attribute = getSupertype().getPluralAttribute( name );
}
basicMapCheck( attribute, name );
return (MapAttribute<? super X, ?, ?>) attribute;
}
@Override
@SuppressWarnings("unchecked")
public MapAttribute<X, ?, ?> getDeclaredMap(String name) {
final PluralAttribute<X,?,?> attribute = declaredPluralAttributes.get( name );
basicMapCheck( attribute, name );
return ( MapAttribute<X,?,?> ) attribute;
}
@Override
@SuppressWarnings({ "unchecked" })
public <K, V> MapAttribute<? super X, K, V> getMap(String name, Class<K> keyType, Class<V> valueType) {
PluralAttribute<? super X, ?, ?> attribute = getPluralAttribute( name );
if ( attribute == null && getSupertype() != null ) {
attribute = getSupertype().getPluralAttribute( name );
}
checkMapValueType( attribute, name, valueType );
final MapAttribute<? super X, K, V> mapAttribute = ( MapAttribute<? super X, K, V> ) attribute;
checkMapKeyType( mapAttribute, name, keyType );
return mapAttribute;
}
@Override
@SuppressWarnings("unchecked")
public <K, V> MapAttribute<X, K, V> getDeclaredMap(String name, Class<K> keyType, Class<V> valueType) {
final PluralAttribute<X,?,?> attribute = declaredPluralAttributes.get( name );
checkMapValueType( attribute, name, valueType );
final MapAttribute<X, K, V> mapAttribute = ( MapAttribute<X, K, V> ) attribute;
checkMapKeyType( mapAttribute, name, keyType );
return mapAttribute;
}
public MapEntryExpression(
CriteriaBuilderImpl criteriaBuilder,
Class<Map.Entry<K, V>> javaType,
PathImplementor origin,
MapAttribute<?, K, V> attribute) {
super( criteriaBuilder, javaType);
this.origin = origin;
this.attribute = attribute;
}
private <K, V> MapJoinImplementor<X, K, V> constructJoin(MapAttribute<? super X, K, V> map, JoinType jt) {
if ( jt.equals( JoinType.RIGHT ) ) {
throw new UnsupportedOperationException( "RIGHT JOIN not supported" );
}
// TODO : runtime check that the attribute in fact belongs to this From's model/bindable
final Class<V> attributeType = map.getBindableJavaType();
return new MapAttributeJoin<X, K, V>( criteriaBuilder(), attributeType, this, map, jt );
}
@Override
@SuppressWarnings({"unchecked"})
public <X, Y> Join<X, Y> join(String attributeName, JoinType jt) {
if ( !canBeJoinSource() ) {
throw illegalJoin();
}
if ( jt.equals( JoinType.RIGHT ) ) {
throw new UnsupportedOperationException( "RIGHT JOIN not supported" );
}
final Attribute<X, ?> attribute = (Attribute<X, ?>) locateAttribute( attributeName );
if ( attribute.isCollection() ) {
final PluralAttribute pluralAttribute = (PluralAttribute) attribute;
if ( PluralAttribute.CollectionType.COLLECTION.equals( pluralAttribute.getCollectionType() ) ) {
return (Join<X, Y>) join( (CollectionAttribute) attribute, jt );
}
else if ( PluralAttribute.CollectionType.LIST.equals( pluralAttribute.getCollectionType() ) ) {
return (Join<X, Y>) join( (ListAttribute) attribute, jt );
}
else if ( PluralAttribute.CollectionType.SET.equals( pluralAttribute.getCollectionType() ) ) {
return (Join<X, Y>) join( (SetAttribute) attribute, jt );
}
else {
return (Join<X, Y>) join( (MapAttribute) attribute, jt );
}
}
else {
return (Join<X, Y>) join( (SingularAttribute) attribute, jt );
}
}
@Override
@SuppressWarnings({"unchecked"})
public <X, K, V> MapJoin<X, K, V> joinMap(String attributeName, JoinType jt) {
final Attribute<X, ?> attribute = (Attribute<X, ?>) locateAttribute( attributeName );
if ( !attribute.isCollection() ) {
throw new IllegalArgumentException( "Requested attribute was not a map" );
}
final PluralAttribute pluralAttribute = (PluralAttribute) attribute;
if ( !PluralAttribute.CollectionType.MAP.equals( pluralAttribute.getCollectionType() ) ) {
throw new IllegalArgumentException( "Requested attribute was not a map" );
}
return (MapJoin<X, K, V>) join( (MapAttribute) attribute, jt );
}
public MapKeySource(
CriteriaBuilderImpl criteriaBuilder,
Class<Map<K, V>> javaType,
MapJoinImplementor<?,K,V> mapJoin,
MapAttribute<?,K,V> attribute) {
super( criteriaBuilder, javaType, null );
this.mapJoin = mapJoin;
this.mapAttribute = attribute;
}
@Override
@SuppressWarnings({ "unchecked" })
public <K, V, M extends Map<K, V>> Expression<M> get(MapAttribute<X, K, V> attribute) {
if ( ! canBeDereferenced() ) {
throw illegalDereference();
}
PluralAttributePath path = (PluralAttributePath) resolveCachedAttributePath( attribute.getName() );
if ( path == null ) {
path = new PluralAttributePath( criteriaBuilder(), this, attribute );
registerAttributePath( attribute.getName(), path );
}
return path;
}
public MapAttributeJoin(
CriteriaBuilderImpl criteriaBuilder,
Class<V> javaType,
PathSource<O> pathSource,
MapAttribute<? super O, K, V> joinAttribute,
JoinType joinType) {
super( criteriaBuilder, javaType, pathSource, joinAttribute, joinType );
}
@SuppressWarnings("unchecked")
public TreatedMapAttributeJoin(MapAttributeJoin<O, K, ? super T> original, Class<T> treatAsType) {
super(
original.criteriaBuilder(),
treatAsType,
original.getPathSource(),
(MapAttribute<? super O, K, T>) original.getAttribute(),
original.getJoinType()
);
this.original = original;
this.treatAsType = treatAsType;
}
private void basicMapCheck(PluralAttribute<? super X, ?, ?> attribute, String name) {
checkNotNull( "MapAttribute", attribute, name );
if ( ! MapAttribute.class.isAssignableFrom( attribute.getClass() ) ) {
throw new IllegalArgumentException( name + " is not a MapAttribute: " + attribute.getClass() );
}
}
private <K,V> void checkMapKeyType(MapAttribute<? super X, K, V> mapAttribute, String name, Class<K> keyType) {
if ( mapAttribute.getKeyJavaType() != keyType ) {
throw new IllegalArgumentException( "MapAttribute named " + name + " does not support a key of type " + keyType );
}
}
public MapAttribute<?, K, V> getAttribute() {
return attribute;
}
@Override
public <K, V> MapJoin<X, K, V> join(MapAttribute<? super X, K, V> map) {
return join( map, DEFAULT_JOIN_TYPE );
}
public MapAttribute<?,K,V> getAttribute() {
return mapAttribute;
}
private String determineRole(MapAttribute<?,K,?> attribute) {
return attribute.getDeclaringType().getJavaType().getName() +
'.' + attribute.getName();
}
@Override
public MapAttribute<? super O, K, V> getAttribute() {
return (MapAttribute<? super O, K, V>) super.getAttribute();
}
@Override
public MapAttribute<? super O, K, V> getModel() {
return getAttribute();
}
@Override
public MapAttribute<X, ?, ?> getDeclaredMap(final String arg0) {
return null;
}
@Override
public <K, V> MapAttribute<X, K, V> getDeclaredMap(final String arg0, final Class<K> arg1, final Class<V> arg2) {
return null;
}
@Override
public MapAttribute<? super X, ?, ?> getMap(final String arg0) {
return null;
}
@Override
public <K, V> MapAttribute<? super X, K, V> getMap(final String arg0, final Class<K> arg1, final Class<V> arg2) {
return null;
}
@Override
public MapAttribute<X, ?, ?> getDeclaredMap(final String arg0) {
return null;
}
@Override
public <K, V> MapAttribute<X, K, V> getDeclaredMap(final String arg0, final Class<K> arg1, final Class<V> arg2) {
return null;
}
@Override
public MapAttribute<? super X, ?, ?> getMap(final String arg0) {
return null;
}
@Override
public <K, V> MapAttribute<? super X, K, V> getMap(final String arg0, final Class<K> arg1, final Class<V> arg2) {
return null;
}