HHH-10885 - Add test for issue

This commit is contained in:
Andrea Boriero 2016-06-23 14:50:39 +02:00
parent 0b34910b22
commit e99270c332
2 changed files with 146 additions and 6 deletions

View File

@ -0,0 +1,140 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.jpa.test.query;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Query;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.hibernate.testing.TestForIssue;
import static org.junit.Assert.assertEquals;
/**
* @author Andrea Boriero
*/
@TestForIssue(jiraKey = "HHH-10885")
public class NativeQueryOrdinalParametersTest extends BaseEntityManagerFunctionalTestCase {
private static final String[] GAME_TITLES = { "Super Mario Brothers", "Mario Kart", "F-Zero" };
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] {Game.class};
}
@Before
public void setUp(){
EntityManager em = getOrCreateEntityManager();
try {
em.getTransaction().begin();
for ( String title : GAME_TITLES ) {
Game game = new Game( title );
em.persist( game );
}
em.getTransaction().commit();
}catch (Exception e){
if(em.getTransaction().isActive()){
em.getTransaction().rollback();
}
throw e;
}finally {
em.close();
}
}
@After
public void tearDown(){
EntityManager em = getOrCreateEntityManager();
try {
em.getTransaction().begin();
em.createQuery( "delete from Game" ).executeUpdate();
em.getTransaction().commit();
}catch (Exception e){
if(em.getTransaction().isActive()){
em.getTransaction().rollback();
}
throw e;
}finally {
em.close();
}
}
@Test
public void testNativeQueryIndexedOrdinalParameter() {
EntityManager em = getOrCreateEntityManager();
try {
Query query = em.createNativeQuery( "SELECT * FROM Game g WHERE title = ?1" );
query.setParameter( 1, "Super Mario Brothers" );
List list = query.getResultList();
assertEquals( 1, list.size() );
}
finally {
em.close();
}
}
@Test
public void testNativeQueryOrdinalParameter() {
EntityManager em = getOrCreateEntityManager();
try {
Query query = em.createNativeQuery( "SELECT * FROM Game g WHERE title = ?" );
query.setParameter( 1, "Super Mario Brothers" );
List list = query.getResultList();
assertEquals( 1, list.size() );
}
finally {
em.close();
}
}
@Entity(name = "Game")
@Table(name = "GAME")
public static class Game {
private Long id;
private String title;
public Game() {
}
public Game(String title) {
this.title = title;
}
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@NotNull
@Size(min = 3, max = 50)
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
}

View File

@ -389,16 +389,16 @@ public class QueryTest extends BaseEntityManagerFunctionalTestCase {
// native queries don't seem to flush by default ?!?
em.flush();
Query q = em.createNativeQuery( "select * from Item i where i.intVal=?" );
q.setParameter( 0, null );
q.setParameter( 1, null );
List results = q.getResultList();
// null != null
assertEquals( 0, results.size() );
q = em.createNativeQuery( "select * from Item i where i.intVal is null and ? is null" );
q.setParameter( 0, null );
q.setParameter( 1, null );
results = q.getResultList();
assertEquals( 1, results.size() );
q = em.createNativeQuery( "select * from Item i where i.intVal is null or i.intVal = ?" );
q.setParameter( 0, null );
q.setParameter(1, null );
results = q.getResultList();
assertEquals( 1, results.size() );
}
@ -436,7 +436,7 @@ public class QueryTest extends BaseEntityManagerFunctionalTestCase {
@Override
public Integer getPosition() {
return 0;
return 1;
}
@Override
@ -1157,7 +1157,7 @@ public class QueryTest extends BaseEntityManagerFunctionalTestCase {
em.getTransaction().commit();
em.getTransaction().begin();
Query query = em.createNativeQuery( "select * from Wallet w where w.brand = ?", Wallet.class );
query.setParameter( 0, "Lacoste" );
query.setParameter( 1, "Lacoste" );
w = (Wallet) query.getSingleResult();
assertNotNull( w );
em.remove( w );
@ -1193,7 +1193,7 @@ public class QueryTest extends BaseEntityManagerFunctionalTestCase {
assertNotNull( item );
assertEquals( "Micro$oft mouse", item.getDescr() );
query = em.createNativeQuery( "select * from Item where name = ?", Item.class );
query.setParameter( 0, "Mouse" );
query.setParameter( 1, "Mouse" );
item = (Item) query.getSingleResult();
assertNotNull( item );
assertEquals( "Micro$oft mouse", item.getDescr() );