HHH-8885 MapJoin.key() produces unusable Path object

This commit is contained in:
Dimitris Mandalidis 2014-04-29 20:44:01 +03:00 committed by Brett Meyer
parent 9d46f517dd
commit ed9b1af83f
4 changed files with 121 additions and 0 deletions

View File

@ -36,8 +36,10 @@ import javax.persistence.metamodel.MapAttribute;
import javax.persistence.metamodel.SingularAttribute;
import javax.persistence.metamodel.Type;
import org.hibernate.jpa.criteria.compile.RenderingContext;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.jpa.criteria.CriteriaBuilderImpl;
import org.hibernate.jpa.criteria.PathSource;
import org.hibernate.jpa.criteria.MapJoinImplementor;
import org.hibernate.jpa.criteria.PathImplementor;
import org.hibernate.persister.collection.CollectionPersister;
@ -109,6 +111,20 @@ public class MapKeyHelpers {
// todo : if key is an entity, this is probably not enough
return (MapKeyPath<T>) this;
}
@Override
public String render(RenderingContext renderingContext) {
PathSource<?> source = getPathSource();
String name;
if ( source != null ) {
source.prepareAlias( renderingContext );
name = source.getPathIdentifier();
}
else {
name = getAttribute().getName();
}
return "key(" + name + ")";
}
}
/**
@ -164,6 +180,12 @@ public class MapKeyHelpers {
public <T extends Map<K, V>> PathImplementor<T> treatAs(Class<T> treatAsType) {
throw new UnsupportedOperationException();
}
@Override
public String getPathIdentifier() {
return mapJoin.getPathIdentifier();
}
}
/**

View File

@ -0,0 +1,45 @@
package org.hibernate.jpa.test.criteria.mapjoin;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.MapJoin;
import javax.persistence.criteria.Root;
import org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest;
import org.hibernate.jpa.test.metamodel.MapEntity;
import org.hibernate.jpa.test.metamodel.MapEntityLocal;
import org.hibernate.jpa.test.metamodel.MapEntityLocal_;
import org.hibernate.jpa.test.metamodel.MapEntity_;
import org.junit.Test;
public class MapJoinTest extends AbstractMetamodelSpecificTest {
@Override
public Class[] getAnnotatedClasses() {
return new Class[] { MapEntity.class, MapEntityLocal.class };
}
@Test
public void allEntities() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<MapEntity> query = cb.createQuery(MapEntity.class);
Root<MapEntity> entity = query.from(MapEntity.class);
MapJoin<MapEntity, String, MapEntityLocal> cname = entity.join(MapEntity_.localized);
query = query
.select(entity)
.where(
cb.equal( cname.key(), "en" )
)
.orderBy( cb.asc( cb.upper( cname.value().get(MapEntityLocal_.shortName) ) ) );
em.createQuery(query).getResultList();
em.getTransaction().commit();
em.close();
}
}

View File

@ -0,0 +1,35 @@
package org.hibernate.jpa.test.metamodel;
import java.util.Map;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.MapKeyColumn;
import javax.persistence.Table;
@Entity
@Table( name = "MAP_ENTITY" )
public class MapEntity {
@Id
@Column(name="key_")
private String key;
@ElementCollection(fetch=FetchType.LAZY)
@CollectionTable(name="MAP_ENTITY_NAME", joinColumns=@JoinColumn(name="key_"))
@MapKeyColumn(name="lang_")
private Map<String, MapEntityLocal> localized;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
}

View File

@ -0,0 +1,19 @@
package org.hibernate.jpa.test.metamodel;
import javax.persistence.Column;
import javax.persistence.Embeddable;
@Embeddable
public class MapEntityLocal {
@Column(name="short_name")
private String shortName;
public String getShortName() {
return shortName;
}
public void setShortName(String shortName) {
this.shortName = shortName;
}
}