Skip to content

Commit

Permalink
HHH-18864 - Creating NativeQuery with result-class leads to problems …
Browse files Browse the repository at this point in the history
…with addEntity and addRoot
  • Loading branch information
sebersole committed Nov 19, 2024
1 parent 707c3a7 commit d29b842
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,46 @@
package org.hibernate.orm.test.sql.hand;


import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;

/**
* @author Emmanuel Bernard
*/
public class Speech {
@Entity
public class Speech implements SpeechInterface {
@Id @GeneratedValue
private Integer id;
private String name;
private Double length;

@Override
public Integer getId() {
return id;
}

@Override
public void setId(Integer id) {
this.id = id;
}

@Override
public Double getLength() {
return length;
}

@Override
public void setLength(Double length) {
this.length = length;
}

@Override
public String getName() {
return name;
}

@Override
public void setName(String name) {
this.name = name;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.sql.hand;

/**
* @author Steve Ebersole
*/
public interface SpeechInterface {
Integer getId();
void setId(Integer id);

Double getLength();
void setLength(Double length);

String getName();
void setName(String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.sql.hand.query;

import org.hibernate.orm.test.sql.hand.Speech;
import org.hibernate.orm.test.sql.hand.SpeechInterface;
import org.hibernate.query.NativeQuery;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.Jira;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.NotImplementedYet;
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 java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* @author Steve Ebersole
*/
@SuppressWarnings("JUnitMalformedDeclaration")
@DomainModel(annotatedClasses = {SpeechInterface.class, Speech.class})
@SessionFactory
@Jira("https://hibernate.atlassian.net/browse/HHH-18864")
public class EntityReturnClassTests {

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// no return-class -> no problem

@Test
@JiraKey("HHH-18864")
public void testAddEntityNoReturn(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
//noinspection unchecked,deprecation
NativeQuery<Speech> query = session.createNativeQuery( "select {s.*} from Speech s" );
query.addEntity("s", Speech.class);
List<Speech> l = query.list();
assertEquals( l.size(), 1 );
} );
}

@Test
@JiraKey("HHH-18864")
public void testAddRootNoReturn(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
//noinspection unchecked,deprecation
NativeQuery<Speech> query = session.createNativeQuery( "select {s.*} from Speech s" );
query.addRoot("s", Speech.class);
List<Speech> l = query.list();
assertEquals( l.size(), 1 );
} );
}


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// entity return-class -> problems with 2 `ResultBuilder` refs

@Test
@JiraKey("HHH-18864")
@NotImplementedYet
public void testAddEntityWithEntityReturn(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
NativeQuery<Speech> query = session.createNativeQuery( "select {s.*} from Speech s", Speech.class );
query.addEntity("s", Speech.class);
List<Speech> l = query.list();
assertEquals( l.size(), 1 );
} );
}

@Test
@JiraKey("HHH-18864")
@NotImplementedYet
public void testAddRootWithEntityReturn(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
NativeQuery<Speech> query = session.createNativeQuery( "select {s.*} from Speech s", Speech.class );
query.addRoot("s", Speech.class);
List<Speech> l = query.list();
assertEquals( l.size(), 1 );
} );
}


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// entity-interface return-class -> problems with `JdbcType` determination

@Test
@JiraKey("HHH-18864")
@NotImplementedYet
public void testAddEntityWithInterfaceReturn(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
NativeQuery<SpeechInterface> query = session.createNativeQuery( "select {s.*} from Speech s", SpeechInterface.class );
query.addEntity("s", Speech.class);
List<SpeechInterface> l = query.list();
assertEquals( l.size(), 1 );
} );
}

@Test
@JiraKey("HHH-18864")
@NotImplementedYet
public void testAddRootWithInterfaceReturn(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
NativeQuery<SpeechInterface> query = session.createNativeQuery( "select {s.*} from Speech s", SpeechInterface.class );
query.addRoot("s", Speech.class);
List<SpeechInterface> l = query.list();
assertEquals( l.size(), 1 );
} );
}


@BeforeEach
void prepareTestData(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
Speech speech = new Speech();
speech.setLength( 23d );
speech.setName( "Mine" );
session.persist( speech );
} );
}

@AfterEach
void dropTestData(SessionFactoryScope scope) {
scope.inTransaction( (session) -> session.createMutationQuery( "delete Speech" ).executeUpdate() );
}
}

0 comments on commit d29b842

Please sign in to comment.