Fix Cannot resolve path with EmbeddedId hql queries not using aliases

This commit is contained in:
Andrea Boriero 2021-03-18 09:42:29 +01:00
parent 31b1627baa
commit 86c024a245
2 changed files with 199 additions and 4 deletions

View File

@ -10,7 +10,6 @@ import java.lang.reflect.Field;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.metamodel.model.domain.EntityDomainType;
import org.hibernate.query.QueryLogging;
import org.hibernate.query.hql.HqlLogging;
import org.hibernate.query.hql.spi.DotIdentifierConsumer;
import org.hibernate.query.hql.spi.SemanticPathPart;
@ -30,8 +29,6 @@ import org.hibernate.type.descriptor.java.EnumJavaTypeDescriptor;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.java.spi.JavaTypeDescriptorRegistry;
import org.jboss.logging.Logger;
/**
* @asciidoc
*
@ -154,7 +151,7 @@ public class BasicDotIdentifierConsumer implements DotIdentifierConsumer {
}
else {
pathRootByExposedNavigable.registerImplicitJoinPath( sqmPath );
return new DomainPathPart( pathRootByExposedNavigable );
return new DomainPathPart( sqmPath );
}
}
}

View File

@ -0,0 +1,198 @@
package org.hibernate.orm.test.query.hql;
import java.io.Serializable;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
@DomainModel(
annotatedClasses = {
EmbeddedIdTest.PersonDocument.class,
EmbeddedIdTest.Document.class,
EmbeddedIdTest.Person.class,
}
)
@SessionFactory
public class EmbeddedIdTest {
@BeforeEach
public void setUp(SessionFactoryScope scope) {
scope.inTransaction( entityManager -> {
final Person person = new Person( 1, "Chris" );
final Document document = new Document( 1, "DL" );
final PersonDocument pd = new PersonDocument( person, document );
entityManager.persist( person );
entityManager.persist( document );
entityManager.persist( pd );
} );
}
@AfterEach
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "delete from PersonDocument" ).executeUpdate();
session.createQuery( "delete from Person" ).executeUpdate();
session.createQuery( "delete from Document" ).executeUpdate();
}
);
}
@Test
public void testHqlQueryWithAlias(SessionFactoryScope scope) {
scope.inTransaction( entityManager -> {
final PersonDocument pd = entityManager
.createQuery(
"FROM PersonDocument p WHERE p.id.person.id = :person",
PersonDocument.class
)
.setParameter( "person", 1 )
.getSingleResult();
assertThat( pd, is( notNullValue() ) );
} );
}
@Test
public void testHqlQueryWithoutAlias(SessionFactoryScope scope) {
scope.inTransaction( entityManager -> {
final PersonDocument pd = entityManager
.createQuery(
"FROM PersonDocument WHERE id.person.id = :person",
PersonDocument.class
)
.setParameter( "person", 1 )
.getSingleResult();
assertThat( pd, is( notNullValue() ) );
} );
}
@Embeddable
public static class PersonDocumentId implements Serializable {
@ManyToOne(optional = false)
private Document document;
@ManyToOne(optional = false)
private Person person;
PersonDocumentId() {
}
PersonDocumentId(Person person, Document document) {
this.person = person;
this.document = document;
}
public Document getDocument() {
return document;
}
public void setDocument(Document document) {
this.document = document;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
@Entity(name = "PersonDocument")
public static class PersonDocument {
@EmbeddedId
private PersonDocumentId id;
PersonDocument() {
}
PersonDocument(Person person, Document document) {
this.id = new PersonDocumentId( person, document );
}
public PersonDocumentId getId() {
return id;
}
public void setId(PersonDocumentId id) {
this.id = id;
}
}
@Entity(name = "Document")
public static class Document {
@Id
private Integer id;
private String name;
Document() {
}
Document(Integer id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Entity(name = "Person")
public static class Person {
@Id
private Integer id;
private String name;
Person() {
}
Person(Integer id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}