HHH-6103 hibernate-entitymanager/src/test/java/org/hibernate/ejb/metamodel/Employee.java

This commit is contained in:
Strong Liu 2012-08-08 18:11:11 +08:00
parent 4d6ae731a9
commit c421d7014b
4 changed files with 168 additions and 12 deletions

View File

@ -79,6 +79,7 @@ public abstract class AbstractPathImpl<X>
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override
public PathSource<?> getParentPath() { public PathSource<?> getParentPath() {
return getPathSource(); return getPathSource();
} }
@ -94,6 +95,7 @@ public abstract class AbstractPathImpl<X>
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override
public String getPathIdentifier() { public String getPathIdentifier() {
return getPathSource().getPathIdentifier() + "." + getAttribute().getName(); return getPathSource().getPathIdentifier() + "." + getAttribute().getName();
} }
@ -135,6 +137,7 @@ public abstract class AbstractPathImpl<X>
* {@inheritDoc} * {@inheritDoc}
*/ */
@SuppressWarnings({ "unchecked" }) @SuppressWarnings({ "unchecked" })
@Override
public <Y> Path<Y> get(SingularAttribute<? super X, Y> attribute) { public <Y> Path<Y> get(SingularAttribute<? super X, Y> attribute) {
if ( ! canBeDereferenced() ) { if ( ! canBeDereferenced() ) {
throw illegalDereference(); throw illegalDereference();
@ -152,6 +155,7 @@ public abstract class AbstractPathImpl<X>
* {@inheritDoc} * {@inheritDoc}
*/ */
@SuppressWarnings({ "unchecked" }) @SuppressWarnings({ "unchecked" })
@Override
public <E, C extends Collection<E>> Expression<C> get(PluralAttribute<X, C, E> attribute) { public <E, C extends Collection<E>> Expression<C> get(PluralAttribute<X, C, E> attribute) {
if ( ! canBeDereferenced() ) { if ( ! canBeDereferenced() ) {
throw illegalDereference(); throw illegalDereference();
@ -169,6 +173,7 @@ public abstract class AbstractPathImpl<X>
* {@inheritDoc} * {@inheritDoc}
*/ */
@SuppressWarnings({ "unchecked" }) @SuppressWarnings({ "unchecked" })
@Override
public <K, V, M extends Map<K, V>> Expression<M> get(MapAttribute<X, K, V> attribute) { public <K, V, M extends Map<K, V>> Expression<M> get(MapAttribute<X, K, V> attribute) {
if ( ! canBeDereferenced() ) { if ( ! canBeDereferenced() ) {
throw illegalDereference(); throw illegalDereference();
@ -186,6 +191,7 @@ public abstract class AbstractPathImpl<X>
* {@inheritDoc} * {@inheritDoc}
*/ */
@SuppressWarnings({ "unchecked" }) @SuppressWarnings({ "unchecked" })
@Override
public <Y> Path<Y> get(String attributeName) { public <Y> Path<Y> get(String attributeName) {
if ( ! canBeDereferenced() ) { if ( ! canBeDereferenced() ) {
throw illegalDereference(); throw illegalDereference();
@ -241,10 +247,11 @@ public abstract class AbstractPathImpl<X>
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override
public void registerParameters(ParameterRegistry registry) { public void registerParameters(ParameterRegistry registry) {
// none to register // none to register
} }
@Override
public void prepareAlias(CriteriaQueryCompiler.RenderingContext renderingContext) { public void prepareAlias(CriteriaQueryCompiler.RenderingContext renderingContext) {
// Make sure we delegate up to our source (eventually up to the path root) to // Make sure we delegate up to our source (eventually up to the path root) to
// prepare the path properly. // prepare the path properly.
@ -257,6 +264,7 @@ public abstract class AbstractPathImpl<X>
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override
public String render(CriteriaQueryCompiler.RenderingContext renderingContext) { public String render(CriteriaQueryCompiler.RenderingContext renderingContext) {
PathSource<?> source = getPathSource(); PathSource<?> source = getPathSource();
if ( source != null ) { if ( source != null ) {
@ -270,6 +278,7 @@ public abstract class AbstractPathImpl<X>
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override
public String renderProjection(CriteriaQueryCompiler.RenderingContext renderingContext) { public String renderProjection(CriteriaQueryCompiler.RenderingContext renderingContext) {
return render( renderingContext ); return render( renderingContext );
} }

View File

@ -37,8 +37,10 @@ import javax.persistence.metamodel.SingularAttribute;
import javax.persistence.metamodel.Type; import javax.persistence.metamodel.Type;
import org.hibernate.ejb.criteria.CriteriaBuilderImpl; import org.hibernate.ejb.criteria.CriteriaBuilderImpl;
import org.hibernate.ejb.criteria.CriteriaQueryCompiler;
import org.hibernate.ejb.criteria.MapJoinImplementor; import org.hibernate.ejb.criteria.MapJoinImplementor;
import org.hibernate.ejb.criteria.PathImplementor; import org.hibernate.ejb.criteria.PathImplementor;
import org.hibernate.ejb.criteria.PathSource;
import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.persister.collection.CollectionPersister; import org.hibernate.persister.collection.CollectionPersister;
@ -74,7 +76,7 @@ public class MapKeyHelpers {
public MapKeySource getPathSource() { public MapKeySource getPathSource() {
return (MapKeySource) super.getPathSource(); return (MapKeySource) super.getPathSource();
} }
@Override
public MapKeyAttribute<K> getAttribute() { public MapKeyAttribute<K> getAttribute() {
return mapKeyAttribute; return mapKeyAttribute;
} }
@ -89,6 +91,20 @@ public class MapKeyHelpers {
return ! isBasicTypeKey(); return ! isBasicTypeKey();
} }
@Override
public String render(CriteriaQueryCompiler.RenderingContext renderingContext) {
PathSource<?> source = getPathSource();
String name;
if ( source != null ) {
source.prepareAlias( renderingContext );
name = source.getPathIdentifier();
}
else {
name = getAttribute().getName();
}
return "key(" + name + ")";
}
@Override @Override
protected Attribute locateAttributeInternal(String attributeName) { protected Attribute locateAttributeInternal(String attributeName) {
if ( ! canBeDereferenced() ) { if ( ! canBeDereferenced() ) {
@ -98,7 +114,7 @@ public class MapKeyHelpers {
} }
throw new UnsupportedOperationException( "Not yet supported!" ); throw new UnsupportedOperationException( "Not yet supported!" );
} }
@Override
public Bindable<K> getModel() { public Bindable<K> getModel() {
return mapKeyAttribute; return mapKeyAttribute;
} }
@ -127,12 +143,13 @@ public class MapKeyHelpers {
this.mapJoin = mapJoin; this.mapJoin = mapJoin;
this.mapAttribute = attribute; this.mapAttribute = attribute;
} }
@Override
public MapAttribute<?,K,V> getAttribute() { public MapAttribute<?,K,V> getAttribute() {
return mapAttribute; return mapAttribute;
} }
@SuppressWarnings({ "unchecked" }) @SuppressWarnings({ "unchecked" })
@Override
public Bindable<Map<K, V>> getModel() { public Bindable<Map<K, V>> getModel() {
// TODO : ok??? the attribute is in fact bindable, but its type signature is different // TODO : ok??? the attribute is in fact bindable, but its type signature is different
return (Bindable<Map<K, V>>) mapAttribute; return (Bindable<Map<K, V>>) mapAttribute;
@ -143,6 +160,11 @@ public class MapKeyHelpers {
return (PathImplementor<?>) mapJoin.getParentPath(); return (PathImplementor<?>) mapJoin.getParentPath();
} }
@Override
public String getPathIdentifier() {
return mapJoin.getPathIdentifier();
}
@Override @Override
protected boolean canBeDereferenced() { protected boolean canBeDereferenced() {
return false; return false;
@ -186,8 +208,7 @@ public class MapKeyHelpers {
: BindableType.SINGULAR_ATTRIBUTE; : BindableType.SINGULAR_ATTRIBUTE;
String guessedRoleName = determineRole( attribute ); String guessedRoleName = determineRole( attribute );
SessionFactoryImplementor sfi = (SessionFactoryImplementor) SessionFactoryImplementor sfi = criteriaBuilder.getEntityManagerFactory().getSessionFactory();
criteriaBuilder.getEntityManagerFactory().getSessionFactory();
mapPersister = sfi.getCollectionPersister( guessedRoleName ); mapPersister = sfi.getCollectionPersister( guessedRoleName );
if ( mapPersister == null ) { if ( mapPersister == null ) {
throw new IllegalStateException( "Could not locate collection persister [" + guessedRoleName + "]" ); throw new IllegalStateException( "Could not locate collection persister [" + guessedRoleName + "]" );
@ -212,6 +233,7 @@ public class MapKeyHelpers {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override
public String getName() { public String getName() {
// TODO : ??? // TODO : ???
return "map-key"; return "map-key";
@ -220,6 +242,7 @@ public class MapKeyHelpers {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override
public PersistentAttributeType getPersistentAttributeType() { public PersistentAttributeType getPersistentAttributeType() {
return persistentAttributeType; return persistentAttributeType;
} }
@ -227,6 +250,7 @@ public class MapKeyHelpers {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override
public ManagedType<Map<K, ?>> getDeclaringType() { public ManagedType<Map<K, ?>> getDeclaringType() {
// TODO : ??? // TODO : ???
return null; return null;
@ -235,6 +259,7 @@ public class MapKeyHelpers {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override
public Class<K> getJavaType() { public Class<K> getJavaType() {
return attribute.getKeyJavaType(); return attribute.getKeyJavaType();
} }
@ -242,6 +267,7 @@ public class MapKeyHelpers {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override
public Member getJavaMember() { public Member getJavaMember() {
// TODO : ??? // TODO : ???
return null; return null;
@ -250,6 +276,7 @@ public class MapKeyHelpers {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override
public boolean isAssociation() { public boolean isAssociation() {
return mapKeyType.isEntityType(); return mapKeyType.isEntityType();
} }
@ -257,30 +284,31 @@ public class MapKeyHelpers {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override
public boolean isCollection() { public boolean isCollection() {
return false; return false;
} }
@Override
public boolean isId() { public boolean isId() {
return false; return false;
} }
@Override
public boolean isVersion() { public boolean isVersion() {
return false; return false;
} }
@Override
public boolean isOptional() { public boolean isOptional() {
return false; return false;
} }
@Override
public Type<K> getType() { public Type<K> getType() {
return jpaType; return jpaType;
} }
@Override
public BindableType getBindableType() { public BindableType getBindableType() {
return jpaBindableType; return jpaBindableType;
} }
@Override
public Class<K> getBindableJavaType() { public Class<K> getBindableJavaType() {
return jpaBinableJavaType; return jpaBinableJavaType;
} }

View File

@ -0,0 +1,63 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.ejb.criteria.basic;
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.ejb.metamodel.Employee;
import org.hibernate.ejb.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;
public class MapKeyTest extends BaseEntityManagerFunctionalTestCase {
@Test
@TestForIssue(jiraKey = "HHH-6103")
public void testCastToString() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Employee> criteria = builder.createQuery( Employee.class );
Root<Employee> employeeRoot = criteria.from( Employee.class );
criteria.select( employeeRoot );
MapJoin<Employee, String, String> phoneRoot = employeeRoot.joinMap( "phoneNumbers" );
criteria.where( builder.equal( phoneRoot.key(), "HOME" ) );
em.createQuery( criteria ).getResultList();
em.getTransaction().commit();
em.close();
}
@Override
public Class[] getAnnotatedClasses() {
return new Class[] {
Employee.class
};
}
}

View File

@ -0,0 +1,56 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.ejb.metamodel;
import java.util.Map;
import javax.persistence.*;
@Entity
public class Employee implements java.io.Serializable {
@Id
private Long id;
@ElementCollection(fetch=FetchType.LAZY)
@CollectionTable(name="emp_phone")
@MapKeyColumn(name="phone_type")
@Column(name="phone_num")
private Map<String, String> phoneNumbers;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Map<String, String> getPhoneNumbers() {
return phoneNumbers;
}
public void setPhoneNumbers(Map<String, String> phoneNumbers) {
this.phoneNumbers = phoneNumbers;
}
}