mirror of
https://github.com/hibernate/hibernate-orm
synced 2025-02-18 00:55:16 +00:00
HHH-9073 - TREAT downcasting and result restrictions
This commit is contained in:
parent
4a9057f31c
commit
4bb7966102
@ -139,12 +139,21 @@ public <Y> Path<Y> get(SingularAttribute<? super X, Y> attribute) {
|
|||||||
|
|
||||||
SingularAttributePath<Y> path = (SingularAttributePath<Y>) resolveCachedAttributePath( attribute.getName() );
|
SingularAttributePath<Y> path = (SingularAttributePath<Y>) resolveCachedAttributePath( attribute.getName() );
|
||||||
if ( path == null ) {
|
if ( path == null ) {
|
||||||
path = new SingularAttributePath<Y>( criteriaBuilder(), attribute.getJavaType(), this, attribute );
|
path = new SingularAttributePath<Y>(
|
||||||
|
criteriaBuilder(),
|
||||||
|
attribute.getJavaType(),
|
||||||
|
getPathSourceForSubPaths(),
|
||||||
|
attribute
|
||||||
|
);
|
||||||
registerAttributePath( attribute.getName(), path );
|
registerAttributePath( attribute.getName(), path );
|
||||||
}
|
}
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected PathSource getPathSourceForSubPaths() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings({ "unchecked" })
|
@SuppressWarnings({ "unchecked" })
|
||||||
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) {
|
||||||
|
@ -126,5 +126,15 @@ public void prepareAlias(RenderingContext renderingContext) {
|
|||||||
public String render(RenderingContext renderingContext) {
|
public String render(RenderingContext renderingContext) {
|
||||||
return "treat(" + original.render( renderingContext ) + " as " + treatAsType.getName() + ")";
|
return "treat(" + original.render( renderingContext ) + " as " + treatAsType.getName() + ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPathIdentifier() {
|
||||||
|
return "treat(" + getAlias() + " as " + treatAsType.getName() + ")";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected PathSource getPathSourceForSubPaths() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -134,5 +134,15 @@ public void prepareAlias(RenderingContext renderingContext) {
|
|||||||
public String render(RenderingContext renderingContext) {
|
public String render(RenderingContext renderingContext) {
|
||||||
return "treat(" + original.render( renderingContext ) + " as " + treatAsType.getName() + ")";
|
return "treat(" + original.render( renderingContext ) + " as " + treatAsType.getName() + ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPathIdentifier() {
|
||||||
|
return "treat(" + getAlias() + " as " + treatAsType.getName() + ")";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected PathSource getPathSourceForSubPaths() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -155,5 +155,15 @@ public void prepareAlias(RenderingContext renderingContext) {
|
|||||||
public String render(RenderingContext renderingContext) {
|
public String render(RenderingContext renderingContext) {
|
||||||
return "treat(" + original.render( renderingContext ) + " as " + treatAsType.getName() + ")";
|
return "treat(" + original.render( renderingContext ) + " as " + treatAsType.getName() + ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPathIdentifier() {
|
||||||
|
return "treat(" + getAlias() + " as " + treatAsType.getName() + ")";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected PathSource getPathSourceForSubPaths() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,7 @@
|
|||||||
import org.hibernate.jpa.criteria.CriteriaBuilderImpl;
|
import org.hibernate.jpa.criteria.CriteriaBuilderImpl;
|
||||||
import org.hibernate.jpa.criteria.CriteriaSubqueryImpl;
|
import org.hibernate.jpa.criteria.CriteriaSubqueryImpl;
|
||||||
import org.hibernate.jpa.criteria.FromImplementor;
|
import org.hibernate.jpa.criteria.FromImplementor;
|
||||||
|
import org.hibernate.jpa.criteria.PathSource;
|
||||||
import org.hibernate.jpa.criteria.compile.RenderingContext;
|
import org.hibernate.jpa.criteria.compile.RenderingContext;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -137,8 +138,22 @@ public void prepareAlias(RenderingContext renderingContext) {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String render(RenderingContext renderingContext) {
|
public String render(RenderingContext renderingContext) {
|
||||||
|
return getTreatFragment();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String getTreatFragment() {
|
||||||
return "treat(" + original.getAlias() + " as " + treatAsType.getName() + ")";
|
return "treat(" + original.getAlias() + " as " + treatAsType.getName() + ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPathIdentifier() {
|
||||||
|
return getTreatFragment();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected PathSource getPathSourceForSubPaths() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -131,5 +131,15 @@ public void prepareAlias(RenderingContext renderingContext) {
|
|||||||
public String render(RenderingContext renderingContext) {
|
public String render(RenderingContext renderingContext) {
|
||||||
return "treat(" + original.render( renderingContext ) + " as " + treatAsType.getName() + ")";
|
return "treat(" + original.render( renderingContext ) + " as " + treatAsType.getName() + ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPathIdentifier() {
|
||||||
|
return "treat(" + getAlias() + " as " + treatAsType.getName() + ")";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected PathSource getPathSourceForSubPaths() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,22 +23,21 @@
|
|||||||
*/
|
*/
|
||||||
package org.hibernate.jpa.test.criteria;
|
package org.hibernate.jpa.test.criteria;
|
||||||
|
|
||||||
import javax.persistence.*;
|
import java.util.List;
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
import javax.persistence.criteria.CriteriaBuilder;
|
import javax.persistence.criteria.CriteriaBuilder;
|
||||||
import javax.persistence.criteria.CriteriaQuery;
|
import javax.persistence.criteria.CriteriaQuery;
|
||||||
import javax.persistence.criteria.Root;
|
import javax.persistence.criteria.Root;
|
||||||
import javax.persistence.metamodel.EntityType;
|
import javax.persistence.metamodel.EntityType;
|
||||||
|
|
||||||
|
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||||
import org.hibernate.jpa.test.metamodel.Thing;
|
import org.hibernate.jpa.test.metamodel.Thing;
|
||||||
import org.hibernate.jpa.test.metamodel.ThingWithQuantity;
|
import org.hibernate.jpa.test.metamodel.ThingWithQuantity;
|
||||||
import org.hibernate.jpa.test.metamodel.ThingWithQuantity_;
|
import org.hibernate.jpa.test.metamodel.ThingWithQuantity_;
|
||||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Steve Ebersole
|
* @author Steve Ebersole
|
||||||
*/
|
*/
|
||||||
@ -110,6 +109,26 @@ public void treatPathClassTest() {
|
|||||||
em.close();
|
em.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void treatPathClassTestHqlControl() {
|
||||||
|
EntityManager em = getOrCreateEntityManager();
|
||||||
|
em.getTransaction().begin();
|
||||||
|
Animal animal = new Animal();
|
||||||
|
animal.setId(100L);
|
||||||
|
animal.setName("2");
|
||||||
|
em.persist(animal);
|
||||||
|
Human human = new Human();
|
||||||
|
human.setId(200L);
|
||||||
|
human.setName("2");
|
||||||
|
em.persist(human);
|
||||||
|
em.getTransaction().commit();
|
||||||
|
|
||||||
|
List<String> animalList = em.createQuery( "select a.name from Animal a where treat (a as Human).name like '2%'" ).getResultList();
|
||||||
|
Assert.assertEquals("treat(Animal as Human) was ignored",1, animalList.size());
|
||||||
|
|
||||||
|
em.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user