HHH-15406 Tests annotated with org.hibernate.testing.RequiresDialect having as value a deprecated dialect are not executed

This commit is contained in:
Andrea Boriero 2022-07-20 13:16:41 +02:00 committed by Andrea Boriero
parent 38589b5e7b
commit 0f599da6c4
17 changed files with 562 additions and 528 deletions

View File

@ -15,32 +15,30 @@ import jakarta.persistence.Inheritance;
import jakarta.persistence.InheritanceType;
import org.hibernate.annotations.DiscriminatorFormula;
import org.hibernate.dialect.PostgreSQL81Dialect;
import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase;
import org.hibernate.dialect.PostgreSQLDialect;
import org.hibernate.testing.RequiresDialect;
import org.junit.Assert;
import org.junit.Test;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.junit.jupiter.api.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* @author Vlad Mihalcea
*/
public class SingleTableDiscriminatorFormulaTest extends BaseEntityManagerFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
DebitAccount.class,
CreditAccount.class,
};
}
@Jpa(
annotatedClasses = {
SingleTableDiscriminatorFormulaTest.DebitAccount.class,
SingleTableDiscriminatorFormulaTest.CreditAccount.class,
}
)
public class SingleTableDiscriminatorFormulaTest {
@Test
@RequiresDialect({PostgreSQL81Dialect.class})
public void test() {
doInJPA(this::entityManagerFactory, entityManager -> {
@RequiresDialect(value = PostgreSQLDialect.class, majorVersion = 8, minorVersion = 1)
public void test(EntityManagerFactoryScope scope) {
scope.inTransaction(entityManager -> {
DebitAccount debitAccount = new DebitAccount("123-debit");
debitAccount.setId(1L);
debitAccount.setOwner("John Doe");
@ -59,10 +57,10 @@ public class SingleTableDiscriminatorFormulaTest extends BaseEntityManagerFuncti
entityManager.persist(creditAccount);
});
doInJPA(this::entityManagerFactory, entityManager -> {
scope.inTransaction(entityManager -> {
List<Account> accounts =
entityManager.createQuery("select a from Account a").getResultList();
Assert.assertEquals(2, accounts.size());
assertEquals(2, accounts.size());
});
}

View File

@ -23,38 +23,51 @@ import jakarta.persistence.PessimisticLockScope;
import org.hibernate.LockMode;
import org.hibernate.LockOptions;
import org.hibernate.Session;
import org.hibernate.dialect.Oracle8iDialect;
import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase;
import org.hibernate.dialect.OracleDialect;
import org.hibernate.query.Query;
import org.hibernate.testing.RequiresDialect;
import org.junit.Test;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.jboss.logging.Logger;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.Assert.assertEquals;
/**
* @author Vlad Mihalcea
*/
public class ExplicitLockingTest extends BaseEntityManagerFunctionalTestCase {
@Jpa(
annotatedClasses = {
ExplicitLockingTest.Person.class,
ExplicitLockingTest.Phone.class,
}
)
public class ExplicitLockingTest {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Person.class,
Phone.class,
};
protected final Logger log = Logger.getLogger( getClass() );
@AfterEach
public void tearDown(EntityManagerFactoryScope scope) {
scope.inTransaction(
entityManager ->
entityManager.createQuery( "delete from Person" ).executeUpdate()
);
}
@Test
public void testJPALockTimeout() {
doInJPA(this::entityManagerFactory, entityManager -> {
public void testJPALockTimeout(EntityManagerFactoryScope scope) {
Person p = scope.fromTransaction( entityManager -> {
Person person = new Person("John Doe");
entityManager.persist(person);
return person;
});
doInJPA(this::entityManagerFactory, entityManager -> {
scope.inTransaction( entityManager -> {
log.info("testJPALockTimeout");
Long id = 1L;
Long id = p.getId();
//tag::locking-jpa-query-hints-timeout-example[]
entityManager.find(
Person.class, id, LockModeType.PESSIMISTIC_WRITE,
@ -65,8 +78,8 @@ public class ExplicitLockingTest extends BaseEntityManagerFunctionalTestCase {
}
@Test
public void testJPALockScope() {
doInJPA(this::entityManagerFactory, entityManager -> {
public void testJPALockScope(EntityManagerFactoryScope scope) {
Person p = scope.fromTransaction( entityManager -> {
Person person = new Person("John Doe");
entityManager.persist(person);
Phone home = new Phone("123-456-7890");
@ -74,10 +87,11 @@ public class ExplicitLockingTest extends BaseEntityManagerFunctionalTestCase {
person.getPhones().add(home);
person.getPhones().add(office);
entityManager.persist(person);
return person;
});
doInJPA(this::entityManagerFactory, entityManager -> {
scope.inTransaction( entityManager -> {
log.info("testJPALockScope");
Long id = 1L;
Long id = p.getId();
//tag::locking-jpa-query-hints-scope-example[]
Person person = entityManager.find(
Person.class, id, LockModeType.PESSIMISTIC_WRITE,
@ -91,8 +105,8 @@ public class ExplicitLockingTest extends BaseEntityManagerFunctionalTestCase {
}
@Test
public void testBuildLockRequest() {
doInJPA(this::entityManagerFactory, entityManager -> {
public void testBuildLockRequest(EntityManagerFactoryScope scope) {
Person p = scope.fromTransaction( entityManager -> {
log.info("testBuildLockRequest");
Person person = new Person("John Doe");
Phone home = new Phone("123-456-7890");
@ -101,9 +115,10 @@ public class ExplicitLockingTest extends BaseEntityManagerFunctionalTestCase {
person.getPhones().add(office);
entityManager.persist(person);
entityManager.flush();
return person;
});
doInJPA(this::entityManagerFactory, entityManager -> {
Long id = 1L;
scope.inTransaction( entityManager -> {
Long id = p.getId();
//tag::locking-buildLockRequest-example[]
Person person = entityManager.find(Person.class, id);
Session session = entityManager.unwrap(Session.class);
@ -115,8 +130,8 @@ public class ExplicitLockingTest extends BaseEntityManagerFunctionalTestCase {
//end::locking-buildLockRequest-example[]
});
doInJPA(this::entityManagerFactory, entityManager -> {
Long id = 1L;
scope.inTransaction( entityManager -> {
Long id = p.getId();
//tag::locking-buildLockRequest-scope-example[]
Person person = entityManager.find(Person.class, id);
Session session = entityManager.unwrap(Session.class);
@ -132,9 +147,9 @@ public class ExplicitLockingTest extends BaseEntityManagerFunctionalTestCase {
}
@Test
@RequiresDialect(Oracle8iDialect.class)
public void testFollowOnLocking() {
doInJPA(this::entityManagerFactory, entityManager -> {
@RequiresDialect(value = OracleDialect.class, majorVersion = 8)
public void testFollowOnLocking(EntityManagerFactoryScope scope) {
scope.inTransaction( entityManager -> {
log.info("testBuildLockRequest");
Person person1 = new Person("John Doe");
Person person2 = new Person("Mrs. John Doe");
@ -144,7 +159,7 @@ public class ExplicitLockingTest extends BaseEntityManagerFunctionalTestCase {
entityManager.flush();
});
doInJPA(this::entityManagerFactory, entityManager -> {
scope.inTransaction( entityManager -> {
//tag::locking-follow-on-example[]
List<Person> persons = entityManager.createQuery(
"select DISTINCT p from Person p", Person.class)
@ -153,7 +168,7 @@ public class ExplicitLockingTest extends BaseEntityManagerFunctionalTestCase {
//end::locking-follow-on-example[]
});
doInJPA(this::entityManagerFactory, entityManager -> {
scope.inTransaction( entityManager -> {
//tag::locking-follow-on-secondary-query-example[]
List<Person> persons = entityManager.createQuery(
"select DISTINCT p from Person p", Person.class)
@ -167,7 +182,7 @@ public class ExplicitLockingTest extends BaseEntityManagerFunctionalTestCase {
//end::locking-follow-on-secondary-query-example[]
});
doInJPA(this::entityManagerFactory, entityManager -> {
scope.inTransaction( entityManager -> {
//tag::locking-follow-on-explicit-example[]
List<Person> persons = entityManager.createQuery(
"select p from Person p", Person.class)

View File

@ -13,30 +13,26 @@ import jakarta.persistence.Id;
import org.hibernate.annotations.Generated;
import org.hibernate.annotations.GenerationTime;
import org.hibernate.dialect.SQLServer2005Dialect;
import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.RequiresDialect;
import org.junit.Test;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.junit.jupiter.api.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* @author Vlad Mihalcea
*/
@RequiresDialect(SQLServer2005Dialect.class)
public class GeneratedTest extends BaseEntityManagerFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Person.class
};
}
@RequiresDialect(value = SQLServer2005Dialect.class, majorVersion = 9)
@Jpa(
annotatedClasses = GeneratedTest.Person.class
)
public class GeneratedTest {
@Test
public void test() {
doInJPA(this::entityManagerFactory, entityManager -> {
public void test(EntityManagerFactoryScope scope) {
scope.inTransaction( entityManager -> {
//tag::mapping-generated-Generated-persist-example[]
Person person = new Person();
person.setId(1L);
@ -54,7 +50,7 @@ public class GeneratedTest extends BaseEntityManagerFunctionalTestCase {
assertEquals("John Flávio André Frederico Rúben Artur Doe", person.getFullName());
//end::mapping-generated-Generated-persist-example[]
});
doInJPA(this::entityManagerFactory, entityManager -> {
scope.inTransaction( entityManager -> {
//tag::mapping-generated-Generated-update-example[]
Person person = entityManager.find(Person.class, 1L);
person.setLastName("Doe Jr");

View File

@ -11,37 +11,32 @@ import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import org.hibernate.annotations.RowId;
import org.hibernate.dialect.Oracle8iDialect;
import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase;
import org.hibernate.dialect.OracleDialect;
import org.hibernate.testing.RequiresDialect;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.junit.jupiter.api.Test;
/**
* @author Vlad Mihalcea
*/
@RequiresDialect(Oracle8iDialect.class)
public class RowIdTest extends BaseEntityManagerFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Product.class
};
}
@RequiresDialect(value = OracleDialect.class, majorVersion = 8)
@Jpa(
annotatedClasses = RowIdTest.Product.class
)
public class RowIdTest {
@Test
public void test() {
doInJPA(this::entityManagerFactory, entityManager -> {
public void test(EntityManagerFactoryScope scope) {
scope.inTransaction( entityManager -> {
Product product = new Product();
product.setId(1L);
product.setName("Mobile phone");
product.setNumber("123-456-7890");
entityManager.persist(product);
});
doInJPA(this::entityManagerFactory, entityManager -> {
scope.inTransaction( entityManager -> {
//tag::identifiers-rowid-example[]
Product product = entityManager.find(Product.class, 1L);

View File

@ -9,8 +9,11 @@ package org.hibernate.userguide.schema;
import java.sql.Blob;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.hibernate.annotations.LazyGroup;
import org.hibernate.annotations.NaturalId;
import jakarta.persistence.Basic;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
@ -19,61 +22,7 @@ import jakarta.persistence.Lob;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import org.hibernate.annotations.LazyGroup;
import org.hibernate.annotations.NaturalId;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.dialect.PostgreSQL81Dialect;
import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.RequiresDialect;
import org.junit.Test;
/**
* @author Vlad Mihalcea
*/
public class SchemaGenerationTest extends BaseEntityManagerFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Person.class,
Book.class,
Customer.class
};
}
@Override
protected Map buildSettings() {
Map settings = super.buildSettings();
if (getDialect().getClass().equals(H2Dialect.class)) {
settings.put(
AvailableSettings.HBM2DDL_IMPORT_FILES,
"schema-generation.sql"
);
settings.put(AvailableSettings.HBM2DDL_AUTO, "update");
}
return settings;
}
@Override
protected String[] getMappings() {
if (PostgreSQL81Dialect.class.isAssignableFrom(getDialect().getClass())) {
return new String[] { "org/hibernate/userguide/schema/SchemaGenerationTest.hbm.xml" };
}
return super.getMappings();
}
@Test
@RequiresDialect(H2Dialect.class)
public void testH2() {
}
@Test
@RequiresDialect(PostgreSQL81Dialect.class)
public void testPostgres() {
}
public abstract class BaseSchemaGeneratorTest {
//tag::schema-generation-domain-model-example[]
@Entity(name = "Customer")
public class Customer {
@ -93,7 +42,7 @@ public class SchemaGenerationTest extends BaseEntityManagerFunctionalTestCase {
//Getters and setters are omitted for brevity
//end::schema-generation-domain-model-example[]
//end::schema-generation-domain-model-example[]
public Integer getId() {
return id;
@ -126,7 +75,7 @@ public class SchemaGenerationTest extends BaseEntityManagerFunctionalTestCase {
public void setImage(Blob image) {
this.image = image;
}
//tag::schema-generation-domain-model-example[]
//tag::schema-generation-domain-model-example[]
}
@Entity(name = "Person")
@ -142,7 +91,7 @@ public class SchemaGenerationTest extends BaseEntityManagerFunctionalTestCase {
//Getters and setters are omitted for brevity
//end::schema-generation-domain-model-example[]
//end::schema-generation-domain-model-example[]
public Long getId() {
return id;
@ -163,7 +112,7 @@ public class SchemaGenerationTest extends BaseEntityManagerFunctionalTestCase {
public List<Book> getBooks() {
return books;
}
//tag::schema-generation-domain-model-example[]
//tag::schema-generation-domain-model-example[]
}
@Entity(name = "Book")
@ -182,7 +131,7 @@ public class SchemaGenerationTest extends BaseEntityManagerFunctionalTestCase {
//Getters and setters are omitted for brevity
//end::schema-generation-domain-model-example[]
//end::schema-generation-domain-model-example[]
public Long getId() {
return id;
@ -215,7 +164,7 @@ public class SchemaGenerationTest extends BaseEntityManagerFunctionalTestCase {
public void setIsbn(String isbn) {
this.isbn = isbn;
}
//tag::schema-generation-domain-model-example[]
//tag::schema-generation-domain-model-example[]
}
//end::schema-generation-domain-model-example[]
}

View File

@ -0,0 +1,38 @@
/*
* 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.userguide.schema;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.testing.orm.junit.Jpa;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.hibernate.testing.orm.junit.Setting;
import org.junit.jupiter.api.Test;
/**
* @author Vlad Mihalcea
*/
@RequiresDialect( H2Dialect.class )
@Jpa(
annotatedClasses = {
BaseSchemaGeneratorTest.Person.class,
BaseSchemaGeneratorTest.Book.class,
BaseSchemaGeneratorTest.Customer.class
},
integrationSettings = {
@Setting( name = AvailableSettings.HBM2DDL_IMPORT_FILES, value = "schema-generation.sql"),
@Setting( name = AvailableSettings.HBM2DDL_AUTO, value = "update")
}
)
public class H2SchemaGenerationTest extends BaseSchemaGeneratorTest {
@Test
public void testH2() {
}
}

View File

@ -0,0 +1,34 @@
/*
* 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.userguide.schema;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.PostgreSQLDialect;
import org.hibernate.testing.orm.junit.Jpa;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.hibernate.testing.orm.junit.Setting;
import org.junit.jupiter.api.Test;
@RequiresDialect(value = PostgreSQLDialect.class, majorVersion = 8)
@Jpa(
annotatedClasses = {
BaseSchemaGeneratorTest.Person.class,
BaseSchemaGeneratorTest.Book.class,
BaseSchemaGeneratorTest.Customer.class
},
xmlMappings = "org/hibernate/userguide/schema/SchemaGenerationTest.hbm.xml",
integrationSettings = {
@Setting(name = AvailableSettings.HBM2DDL_IMPORT_FILES, value = "schema-generation.sql"),
@Setting(name = AvailableSettings.HBM2DDL_AUTO, value = "update")
}
)
public class PostgreSQLSchemaGenerationTest extends BaseSchemaGeneratorTest {
@Test
public void testPostgres() {
}
}

View File

@ -8,256 +8,265 @@ import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Pattern;
import jakarta.persistence.ParameterMode;
import jakarta.persistence.StoredProcedureQuery;
import org.hibernate.Session;
import org.hibernate.dialect.MySQL5Dialect;
import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.dialect.TiDBDialect;
import org.hibernate.procedure.ProcedureCall;
import org.hibernate.query.procedure.ProcedureParameter;
import org.hibernate.result.Output;
import org.hibernate.result.ResultSetOutput;
import org.hibernate.userguide.model.Account;
import org.hibernate.userguide.model.AddressType;
import org.hibernate.userguide.model.Call;
import org.hibernate.userguide.model.Partner;
import org.hibernate.userguide.model.Payment;
import org.hibernate.userguide.model.Person;
import org.hibernate.userguide.model.Phone;
import org.hibernate.userguide.model.PhoneType;
import org.hibernate.testing.RequiresDialect;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.hibernate.testing.orm.junit.SkipForDialect;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import jakarta.persistence.ParameterMode;
import jakarta.persistence.StoredProcedureQuery;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author Vlad Mihalcea
*/
@RequiresDialect(MySQL5Dialect.class)
public class MySQLStoredProcedureTest extends BaseEntityManagerFunctionalTestCase {
@RequiresDialect(value = MySQLDialect.class, majorVersion = 5)
@SkipForDialect(dialectClass = TiDBDialect.class, reason = "TiDB doesn't support stored procedures")
@Jpa(
annotatedClasses = {
Person.class,
Partner.class,
Phone.class,
Call.class,
Payment.class,
Account.class
}
)
public class MySQLStoredProcedureTest {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Person.class,
Partner.class,
Phone.class,
Call.class,
};
}
@BeforeAll
public void init(EntityManagerFactoryScope scope) {
destroy( scope );
scope.inTransaction( entityManager -> {
Session session = entityManager.unwrap( Session.class );
session.doWork( connection -> {
try (Statement statement = connection.createStatement()) {
//tag::sql-sp-out-mysql-example[]
statement.executeUpdate(
"CREATE PROCEDURE sp_count_phones (" +
" IN personId INT, " +
" OUT phoneCount INT " +
") " +
"BEGIN " +
" SELECT COUNT(*) INTO phoneCount " +
" FROM Phone p " +
" WHERE p.person_id = personId; " +
"END"
);
//end::sql-sp-out-mysql-example[]
//tag::sql-sp-no-out-mysql-example[]
statement.executeUpdate(
"CREATE PROCEDURE sp_phones(IN personId INT) " +
"BEGIN " +
" SELECT * " +
" FROM Phone " +
" WHERE person_id = personId; " +
"END"
);
//end::sql-sp-no-out-mysql-example[]
//tag::sql-function-mysql-example[]
statement.executeUpdate(
"CREATE FUNCTION fn_count_phones(personId integer) " +
"RETURNS integer " +
"DETERMINISTIC " +
"READS SQL DATA " +
"BEGIN " +
" DECLARE phoneCount integer; " +
" SELECT COUNT(*) INTO phoneCount " +
" FROM Phone p " +
" WHERE p.person_id = personId; " +
" RETURN phoneCount; " +
"END"
);
//end::sql-function-mysql-example[]
}
} );
} );
scope.inTransaction( entityManager -> {
Person person1 = new Person( "John Doe" );
person1.setNickName( "JD" );
person1.setAddress( "Earth" );
person1.setCreatedOn( LocalDateTime.of( 2000, 1, 1, 0, 0, 0 ) );
person1.getAddresses().put( AddressType.HOME, "Home address" );
person1.getAddresses().put( AddressType.OFFICE, "Office address" );
@Before
public void init() {
destroy();
doInJPA(this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap(Session.class);
session.doWork(connection -> {
try(Statement statement = connection.createStatement()) {
//tag::sql-sp-out-mysql-example[]
statement.executeUpdate(
"CREATE PROCEDURE sp_count_phones (" +
" IN personId INT, " +
" OUT phoneCount INT " +
") " +
"BEGIN " +
" SELECT COUNT(*) INTO phoneCount " +
" FROM Phone p " +
" WHERE p.person_id = personId; " +
"END"
);
//end::sql-sp-out-mysql-example[]
//tag::sql-sp-no-out-mysql-example[]
statement.executeUpdate(
"CREATE PROCEDURE sp_phones(IN personId INT) " +
"BEGIN " +
" SELECT * " +
" FROM Phone " +
" WHERE person_id = personId; " +
"END"
);
//end::sql-sp-no-out-mysql-example[]
//tag::sql-function-mysql-example[]
statement.executeUpdate(
"CREATE FUNCTION fn_count_phones(personId integer) " +
"RETURNS integer " +
"DETERMINISTIC " +
"READS SQL DATA " +
"BEGIN " +
" DECLARE phoneCount integer; " +
" SELECT COUNT(*) INTO phoneCount " +
" FROM Phone p " +
" WHERE p.person_id = personId; " +
" RETURN phoneCount; " +
"END"
);
//end::sql-function-mysql-example[]
}
});
});
doInJPA(this::entityManagerFactory, entityManager -> {
Person person1 = new Person("John Doe");
person1.setNickName("JD");
person1.setAddress("Earth");
person1.setCreatedOn(LocalDateTime.of(2000, 1, 1, 0, 0, 0)) ;
person1.getAddresses().put(AddressType.HOME, "Home address");
person1.getAddresses().put(AddressType.OFFICE, "Office address");
entityManager.persist( person1 );
entityManager.persist(person1);
Phone phone1 = new Phone( "123-456-7890" );
phone1.setId( 1L );
phone1.setType( PhoneType.MOBILE );
Phone phone1 = new Phone("123-456-7890");
phone1.setId(1L);
phone1.setType(PhoneType.MOBILE);
person1.addPhone( phone1 );
person1.addPhone(phone1);
Phone phone2 = new Phone( "098_765-4321" );
phone2.setId( 2L );
phone2.setType( PhoneType.LAND_LINE );
Phone phone2 = new Phone("098_765-4321");
phone2.setId(2L);
phone2.setType(PhoneType.LAND_LINE);
person1.addPhone( phone2 );
} );
}
person1.addPhone(phone2);
});
}
@AfterAll
public void destroy(EntityManagerFactoryScope scope) {
scope.inTransaction( entityManager -> {
Session session = entityManager.unwrap( Session.class );
session.doWork( connection -> {
try (Statement statement = connection.createStatement()) {
statement.executeUpdate( "DROP PROCEDURE IF EXISTS sp_count_phones" );
}
catch (SQLException ignore) {
}
} );
} );
scope.inTransaction( entityManager -> {
Session session = entityManager.unwrap( Session.class );
session.doWork( connection -> {
try (Statement statement = connection.createStatement()) {
statement.executeUpdate( "DROP PROCEDURE IF EXISTS sp_phones" );
}
catch (SQLException ignore) {
}
} );
} );
scope.inTransaction( entityManager -> {
Session session = entityManager.unwrap( Session.class );
session.doWork( connection -> {
try (Statement statement = connection.createStatement()) {
statement.executeUpdate( "DROP FUNCTION IF EXISTS fn_count_phones" );
}
catch (SQLException ignore) {
}
} );
} );
}
@After
public void destroy() {
doInJPA(this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap(Session.class);
session.doWork(connection -> {
try(Statement statement = connection.createStatement()) {
statement.executeUpdate("DROP PROCEDURE IF EXISTS sp_count_phones");
}
catch (SQLException ignore) {
}
});
});
doInJPA(this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap(Session.class);
session.doWork(connection -> {
try(Statement statement = connection.createStatement()) {
statement.executeUpdate("DROP PROCEDURE IF EXISTS sp_phones");
}
catch (SQLException ignore) {
}
});
});
doInJPA(this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap(Session.class);
session.doWork(connection -> {
try(Statement statement = connection.createStatement()) {
statement.executeUpdate("DROP FUNCTION IF EXISTS fn_count_phones");
}
catch (SQLException ignore) {
}
});
});
}
@Test
public void testStoredProcedureOutParameter(EntityManagerFactoryScope scope) {
scope.inTransaction( entityManager -> {
//tag::sql-jpa-call-sp-out-mysql-example[]
StoredProcedureQuery query = entityManager.createStoredProcedureQuery( "sp_count_phones" );
query.registerStoredProcedureParameter( "personId", Long.class, ParameterMode.IN );
query.registerStoredProcedureParameter( "phoneCount", Long.class, ParameterMode.OUT );
@Test
public void testStoredProcedureOutParameter() {
doInJPA(this::entityManagerFactory, entityManager -> {
//tag::sql-jpa-call-sp-out-mysql-example[]
StoredProcedureQuery query = entityManager.createStoredProcedureQuery("sp_count_phones");
query.registerStoredProcedureParameter("personId", Long.class, ParameterMode.IN);
query.registerStoredProcedureParameter("phoneCount", Long.class, ParameterMode.OUT);
query.setParameter( "personId", 1L );
query.setParameter("personId", 1L);
query.execute();
Long phoneCount = (Long) query.getOutputParameterValue( "phoneCount" );
//end::sql-jpa-call-sp-out-mysql-example[]
assertEquals( 2l, phoneCount );
} );
}
query.execute();
Long phoneCount = (Long) query.getOutputParameterValue("phoneCount");
//end::sql-jpa-call-sp-out-mysql-example[]
assertEquals(Long.valueOf(2), phoneCount);
});
}
@Test
public void testHibernateProcedureCallOutParameter(EntityManagerFactoryScope scope) {
scope.inTransaction( entityManager -> {
//tag::sql-hibernate-call-sp-out-mysql-example[]
Session session = entityManager.unwrap( Session.class );
@Test
public void testHibernateProcedureCallOutParameter() {
doInJPA(this::entityManagerFactory, entityManager -> {
//tag::sql-hibernate-call-sp-out-mysql-example[]
Session session = entityManager.unwrap(Session.class);
ProcedureCall call = session.createStoredProcedureCall( "sp_count_phones" );
ProcedureParameter<Long> parameter = call.registerParameter( "personId", Long.class, ParameterMode.IN );
call.setParameter( parameter, 1L );
call.registerParameter( "phoneCount", Long.class, ParameterMode.OUT );
ProcedureCall call = session.createStoredProcedureCall("sp_count_phones");
ProcedureParameter<Long> parameter = call.registerParameter("personId", Long.class, ParameterMode.IN);
call.setParameter(parameter, 1L);
call.registerParameter("phoneCount", Long.class, ParameterMode.OUT);
Long phoneCount = (Long) call.getOutputs().getOutputParameterValue( "phoneCount" );
assertEquals( Long.valueOf( 2 ), phoneCount );
//end::sql-hibernate-call-sp-out-mysql-example[]
} );
}
Long phoneCount = (Long) call.getOutputs().getOutputParameterValue("phoneCount");
assertEquals(Long.valueOf(2), phoneCount);
//end::sql-hibernate-call-sp-out-mysql-example[]
});
}
@Test
public void testStoredProcedureRefCursor(EntityManagerFactoryScope scope) {
try {
scope.inTransaction( entityManager -> {
StoredProcedureQuery query = entityManager.createStoredProcedureQuery( "sp_phones" );
query.registerStoredProcedureParameter( 1, void.class, ParameterMode.REF_CURSOR );
query.registerStoredProcedureParameter( 2, Long.class, ParameterMode.IN );
@Test
public void testStoredProcedureRefCursor() {
try {
doInJPA(this::entityManagerFactory, entityManager -> {
StoredProcedureQuery query = entityManager.createStoredProcedureQuery("sp_phones");
query.registerStoredProcedureParameter(1, void.class, ParameterMode.REF_CURSOR);
query.registerStoredProcedureParameter(2, Long.class, ParameterMode.IN);
query.setParameter( 2, 1L );
query.setParameter(2, 1L);
List<Object[]> personComments = query.getResultList();
assertEquals( 2, personComments.size() );
} );
}
catch (Exception e) {
assertTrue( Pattern.compile( "Dialect .*? not known to support REF_CURSOR parameters" )
.matcher( e.getCause().getMessage() )
.matches() );
}
}
List<Object[]> personComments = query.getResultList();
assertEquals(2, personComments.size());
});
} catch (Exception e) {
assertTrue(Pattern.compile("Dialect .*? not known to support REF_CURSOR parameters").matcher(e.getCause().getMessage()).matches());
}
}
@Test
public void testStoredProcedureReturnValue(EntityManagerFactoryScope scope) {
scope.inTransaction( entityManager -> {
//tag::sql-jpa-call-sp-no-out-mysql-example[]
StoredProcedureQuery query = entityManager.createStoredProcedureQuery( "sp_phones" );
query.registerStoredProcedureParameter( 1, Long.class, ParameterMode.IN );
@Test
public void testStoredProcedureReturnValue() {
doInJPA(this::entityManagerFactory, entityManager -> {
//tag::sql-jpa-call-sp-no-out-mysql-example[]
StoredProcedureQuery query = entityManager.createStoredProcedureQuery("sp_phones");
query.registerStoredProcedureParameter(1, Long.class, ParameterMode.IN);
query.setParameter( 1, 1L );
query.setParameter(1, 1L);
List<Object[]> personComments = query.getResultList();
//end::sql-jpa-call-sp-no-out-mysql-example[]
assertEquals( 2, personComments.size() );
} );
}
List<Object[]> personComments = query.getResultList();
//end::sql-jpa-call-sp-no-out-mysql-example[]
assertEquals(2, personComments.size());
});
}
@Test
public void testHibernateProcedureCallReturnValueParameter(EntityManagerFactoryScope scope) {
scope.inTransaction( entityManager -> {
//tag::sql-hibernate-call-sp-no-out-mysql-example[]
Session session = entityManager.unwrap( Session.class );
@Test
public void testHibernateProcedureCallReturnValueParameter() {
doInJPA(this::entityManagerFactory, entityManager -> {
//tag::sql-hibernate-call-sp-no-out-mysql-example[]
Session session = entityManager.unwrap(Session.class);
ProcedureCall call = session.createStoredProcedureCall( "sp_phones" );
ProcedureParameter<Long> parameter = call.registerParameter( 1, Long.class, ParameterMode.IN );
call.setParameter( parameter, 1L );
ProcedureCall call = session.createStoredProcedureCall("sp_phones");
ProcedureParameter<Long> parameter = call.registerParameter(1, Long.class, ParameterMode.IN);
call.setParameter(parameter, 1L);
Output output = call.getOutputs().getCurrent();
Output output = call.getOutputs().getCurrent();
List<Object[]> personComments = ( (ResultSetOutput) output ).getResultList();
//end::sql-hibernate-call-sp-no-out-mysql-example[]
assertEquals( 2, personComments.size() );
} );
}
List<Object[]> personComments = ((ResultSetOutput) output).getResultList();
//end::sql-hibernate-call-sp-no-out-mysql-example[]
assertEquals(2, personComments.size());
});
}
@Test
public void testFunctionWithJDBC() {
doInJPA(this::entityManagerFactory, entityManager -> {
//tag::sql-call-function-mysql-example[]
final AtomicReference<Integer> phoneCount = new AtomicReference<>();
Session session = entityManager.unwrap(Session.class);
session.doWork(connection -> {
try (CallableStatement function = connection.prepareCall(
"{ ? = call fn_count_phones(?) }")) {
function.registerOutParameter(1, Types.INTEGER);
function.setInt(2, 1);
function.execute();
phoneCount.set(function.getInt(1));
}
});
//end::sql-call-function-mysql-example[]
assertEquals(Integer.valueOf(2), phoneCount.get());
});
}
@Test
public void testFunctionWithJDBC(EntityManagerFactoryScope scope) {
scope.inTransaction( entityManager -> {
//tag::sql-call-function-mysql-example[]
final AtomicReference<Integer> phoneCount = new AtomicReference<>();
Session session = entityManager.unwrap( Session.class );
session.doWork( connection -> {
try (CallableStatement function = connection.prepareCall(
"{ ? = call fn_count_phones(?) }" )) {
function.registerOutParameter( 1, Types.INTEGER );
function.setInt( 2, 1 );
function.execute();
phoneCount.set( function.getInt( 1 ) );
}
} );
//end::sql-call-function-mysql-example[]
assertEquals( Integer.valueOf( 2 ), phoneCount.get() );
} );
}
}

View File

@ -7,107 +7,106 @@
package org.hibernate.userguide.sql;
import java.sql.Statement;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.NamedNativeQueries;
import jakarta.persistence.NamedNativeQuery;
import org.hibernate.Session;
import org.hibernate.annotations.Loader;
import org.hibernate.annotations.ResultCheckStyle;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLInsert;
import org.hibernate.dialect.Oracle8iDialect;
import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase;
import org.hibernate.dialect.OracleDialect;
import org.hibernate.testing.RequiresDialect;
import org.junit.Before;
import org.junit.Test;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.NamedNativeQueries;
import jakarta.persistence.NamedNativeQuery;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
/**
* @author Vlad Mihalcea
*/
@RequiresDialect(Oracle8iDialect.class)
public class OracleCustomSQLWithStoredProcedureTest extends BaseEntityManagerFunctionalTestCase {
@RequiresDialect(value = OracleDialect.class, majorVersion = 8)
@Jpa(
annotatedClasses = OracleCustomSQLWithStoredProcedureTest.Person.class
)
public class OracleCustomSQLWithStoredProcedureTest {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Person.class
};
}
@Before
public void init() {
doInJPA(this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap(Session.class);
session.doWork(connection -> {
try(Statement statement = connection.createStatement();) {
statement.executeUpdate("ALTER TABLE person ADD valid NUMBER(1) DEFAULT 0 NOT NULL");
@BeforeAll
public void init(EntityManagerFactoryScope scope) {
scope.inTransaction( entityManager -> {
Session session = entityManager.unwrap( Session.class );
session.doWork( connection -> {
try (Statement statement = connection.createStatement();) {
statement.executeUpdate( "ALTER TABLE person ADD valid NUMBER(1) DEFAULT 0 NOT NULL" );
//tag::sql-sp-soft-delete-example[]
statement.executeUpdate(
"CREATE OR REPLACE PROCEDURE sp_delete_person (" +
" personId IN NUMBER) " +
"AS " +
"BEGIN " +
" UPDATE person SET valid = 0 WHERE id = personId; " +
"END;"
);}
"CREATE OR REPLACE PROCEDURE sp_delete_person (" +
" personId IN NUMBER) " +
"AS " +
"BEGIN " +
" UPDATE person SET valid = 0 WHERE id = personId; " +
"END;"
);
}
//end::sql-sp-soft-delete-example[]
});
});
} );
} );
}
@Test
public void test_sql_custom_crud() {
public void test_sql_custom_crud(EntityManagerFactoryScope scope) {
Person _person = doInJPA(this::entityManagerFactory, entityManager -> {
Person _person = scope.fromTransaction( entityManager -> {
Person person = new Person();
person.setName("John Doe");
entityManager.persist(person);
person.setName( "John Doe" );
entityManager.persist( person );
return person;
});
} );
doInJPA(this::entityManagerFactory, entityManager -> {
scope.inTransaction( entityManager -> {
Long postId = _person.getId();
Person person = entityManager.find(Person.class, postId);
assertNotNull(person);
entityManager.remove(person);
});
Person person = entityManager.find( Person.class, postId );
assertNotNull( person );
entityManager.remove( person );
} );
doInJPA(this::entityManagerFactory, entityManager -> {
scope.inTransaction( entityManager -> {
Long postId = _person.getId();
Person person = entityManager.find(Person.class, postId);
assertNull(person);
});
Person person = entityManager.find( Person.class, postId );
assertNull( person );
} );
}
@Entity(name = "Person")
@SQLInsert(
sql = "INSERT INTO person (name, id, valid) VALUES (?, ?, 1) ",
check = ResultCheckStyle.COUNT
sql = "INSERT INTO person (name, id, valid) VALUES (?, ?, 1) ",
check = ResultCheckStyle.COUNT
)
//tag::sql-sp-custom-crud-example[]
@SQLDelete(
sql = "{ call sp_delete_person(?) } ",
callable = true
sql = "{ call sp_delete_person(?) } ",
callable = true
)
//end::sql-sp-custom-crud-example[]
@Loader(namedQuery = "find_valid_person")
@NamedNativeQueries({
@NamedNativeQuery(
name = "find_valid_person",
query = "SELECT id, name " +
"FROM person " +
"WHERE id = ? and valid = 1",
resultClass = Person.class
)
@NamedNativeQuery(
name = "find_valid_person",
query = "SELECT id, name " +
"FROM person " +
"WHERE id = ? and valid = 1",
resultClass = Person.class
)
})
public static class Person {

View File

@ -3,55 +3,57 @@ package org.hibernate.userguide.sql;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;
import jakarta.persistence.ParameterMode;
import jakarta.persistence.StoredProcedureQuery;
import org.hibernate.Session;
import org.hibernate.dialect.Oracle8iDialect;
import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase;
import org.hibernate.dialect.OracleDialect;
import org.hibernate.procedure.ProcedureCall;
import org.hibernate.query.procedure.ProcedureParameter;
import org.hibernate.result.Output;
import org.hibernate.result.ResultSetOutput;
import org.hibernate.userguide.model.Account;
import org.hibernate.userguide.model.AddressType;
import org.hibernate.userguide.model.Call;
import org.hibernate.userguide.model.Partner;
import org.hibernate.userguide.model.Payment;
import org.hibernate.userguide.model.Person;
import org.hibernate.userguide.model.Phone;
import org.hibernate.userguide.model.PhoneType;
import org.hibernate.testing.RequiresDialect;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import jakarta.persistence.ParameterMode;
import jakarta.persistence.StoredProcedureQuery;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* @author Vlad Mihalcea
*/
@RequiresDialect(Oracle8iDialect.class)
public class OracleStoredProcedureTest extends BaseEntityManagerFunctionalTestCase {
@RequiresDialect(value = OracleDialect.class, majorVersion = 8)
@Jpa(
annotatedClasses = {
Person.class,
Partner.class,
Phone.class,
Call.class,
Payment.class,
Account.class
}
)
public class OracleStoredProcedureTest {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Person.class,
Partner.class,
Phone.class,
Call.class,
};
}
@Before
public void init() {
doInJPA(this::entityManagerFactory, entityManager -> {
@BeforeAll
public void init(EntityManagerFactoryScope scope) {
scope.inTransaction( entityManager -> {
Session session = entityManager.unwrap(Session.class);
session.doWork(connection -> {
try(Statement statement = connection.createStatement()) {
@ -96,7 +98,7 @@ public class OracleStoredProcedureTest extends BaseEntityManagerFunctionalTestCa
}
});
});
doInJPA(this::entityManagerFactory, entityManager -> {
scope.inTransaction( entityManager -> {
Person person1 = new Person("John Doe");
person1.setNickName("JD");
person1.setAddress("Earth");
@ -120,9 +122,9 @@ public class OracleStoredProcedureTest extends BaseEntityManagerFunctionalTestCa
});
}
@After
public void destroy() {
doInJPA(this::entityManagerFactory, entityManager -> {
@AfterAll
public void destroy(EntityManagerFactoryScope scope) {
scope.inTransaction( entityManager -> {
Session session = entityManager.unwrap(Session.class);
session.doWork(connection -> {
try(Statement statement = connection.createStatement()) {
@ -132,7 +134,7 @@ public class OracleStoredProcedureTest extends BaseEntityManagerFunctionalTestCa
}
});
});
doInJPA(this::entityManagerFactory, entityManager -> {
scope.inTransaction( entityManager -> {
Session session = entityManager.unwrap(Session.class);
session.doWork(connection -> {
try(Statement statement = connection.createStatement()) {
@ -142,7 +144,7 @@ public class OracleStoredProcedureTest extends BaseEntityManagerFunctionalTestCa
}
});
});
doInJPA(this::entityManagerFactory, entityManager -> {
scope.inTransaction( entityManager -> {
Session session = entityManager.unwrap(Session.class);
session.doWork(connection -> {
try(Statement statement = connection.createStatement()) {
@ -155,8 +157,8 @@ public class OracleStoredProcedureTest extends BaseEntityManagerFunctionalTestCa
}
@Test
public void testStoredProcedureOutParameter() {
doInJPA(this::entityManagerFactory, entityManager -> {
public void testStoredProcedureOutParameter(EntityManagerFactoryScope scope) {
scope.inTransaction( entityManager -> {
StoredProcedureQuery query = entityManager.createStoredProcedureQuery("sp_count_phones");
query.registerStoredProcedureParameter(1, Long.class, ParameterMode.IN);
query.registerStoredProcedureParameter(2, Long.class, ParameterMode.OUT);
@ -165,13 +167,13 @@ public class OracleStoredProcedureTest extends BaseEntityManagerFunctionalTestCa
query.execute();
Long phoneCount = (Long) query.getOutputParameterValue(2);
assertEquals(Long.valueOf(2), phoneCount);
assertEquals(2l, phoneCount);
});
}
@Test
public void testStoredProcedureRefCursor() {
doInJPA(this::entityManagerFactory, entityManager -> {
public void testStoredProcedureRefCursor(EntityManagerFactoryScope scope) {
scope.inTransaction( entityManager -> {
//tag::sql-jpa-call-sp-ref-cursor-oracle-example[]
StoredProcedureQuery query = entityManager.createStoredProcedureQuery("sp_person_phones");
query.registerStoredProcedureParameter(1, Long.class, ParameterMode.IN);
@ -186,8 +188,8 @@ public class OracleStoredProcedureTest extends BaseEntityManagerFunctionalTestCa
}
@Test
public void testStoredProcedureRefCursorUsingNamedQuery() {
doInJPA(this::entityManagerFactory, entityManager -> {
public void testStoredProcedureRefCursorUsingNamedQuery(EntityManagerFactoryScope scope) {
scope.inTransaction( entityManager -> {
//tag::sql-jpa-call-sp-ref-cursor-oracle-named-query-example[]
List<Object[]> postComments = entityManager
.createNamedStoredProcedureQuery("sp_person_phones")
@ -200,8 +202,8 @@ public class OracleStoredProcedureTest extends BaseEntityManagerFunctionalTestCa
}
@Test
public void testHibernateProcedureCallRefCursor() {
doInJPA(this::entityManagerFactory, entityManager -> {
public void testHibernateProcedureCallRefCursor(EntityManagerFactoryScope scope) {
scope.inTransaction( entityManager -> {
//tag::sql-hibernate-call-sp-ref-cursor-oracle-example[]
Session session = entityManager.unwrap(Session.class);
@ -218,18 +220,13 @@ public class OracleStoredProcedureTest extends BaseEntityManagerFunctionalTestCa
}
@Test
public void testStoredProcedureReturnValue() {
try {
doInJPA(this::entityManagerFactory, entityManager -> {
BigDecimal phoneCount = (BigDecimal) entityManager
.createNativeQuery("SELECT fn_count_phones(:personId) FROM DUAL")
.setParameter("personId", 1)
.getSingleResult();
assertEquals(BigDecimal.valueOf(2), phoneCount);
});
}
catch (Exception e) {
e.printStackTrace();
}
public void testStoredProcedureReturnValue(EntityManagerFactoryScope scope) {
scope.inTransaction( entityManager -> {
BigDecimal phoneCount = (BigDecimal) entityManager
.createNativeQuery( "SELECT fn_count_phones(:personId) FROM DUAL" )
.setParameter( "personId", 1 )
.getSingleResult();
assertEquals( BigDecimal.valueOf( 2 ), phoneCount );
} );
}
}

View File

@ -8,41 +8,46 @@ package org.hibernate.test.c3p0;
import java.sql.Statement;
import org.hibernate.dialect.SQLServer2005Dialect;
import org.hibernate.dialect.SQLServerDialect;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
/**
* Tests that checks the JDBC 4.2 compatibility of c3p0
*
* @author Vlad Mihalcea
*/
@RequiresDialect(SQLServer2005Dialect.class)
public class JdbcCompatibilityTest extends BaseCoreFunctionalTestCase {
@RequiresDialect(value = SQLServerDialect.class, majorVersion = 9)
@DomainModel(
annotatedClasses = { IrrelevantEntity.class }
)
@SessionFactory
public class JdbcCompatibilityTest {
@Test
@TestForIssue( jiraKey = "HHH-11308" )
public void testJdbc41() {
doInHibernate( this::sessionFactory, session -> {
@TestForIssue(jiraKey = "HHH-11308")
public void testJdbc41(SessionFactoryScope scope) {
scope.inTransaction( session -> {
session.doWork( connection -> {
//Connection#getSchema was added in Java 1.7
String schema = connection.getSchema();
assertNotNull(schema);
assertNotNull( schema );
} );
} );
}
@Test
@TestForIssue( jiraKey = "HHH-11308" )
public void testJdbc42() {
doInHibernate( this::sessionFactory, session -> {
@TestForIssue(jiraKey = "HHH-11308")
public void testJdbc42(SessionFactoryScope scope) {
scope.inTransaction( session -> {
for ( int i = 0; i < 5; i++ ) {
IrrelevantEntity entity = new IrrelevantEntity();
entity.setName( getClass().getName() );
@ -50,16 +55,12 @@ public class JdbcCompatibilityTest extends BaseCoreFunctionalTestCase {
}
session.flush();
session.doWork( connection -> {
try( Statement statement = connection.createStatement()) {
try (Statement statement = connection.createStatement()) {
statement.executeUpdate( "DELETE FROM IrrelevantEntity" );
assertEquals( 5, statement.getLargeUpdateCount());
assertEquals( 5, statement.getLargeUpdateCount() );
}
} );
} );
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[]{ IrrelevantEntity.class };
}
}

View File

@ -20,7 +20,6 @@ import jakarta.persistence.StoredProcedureParameter;
import jakarta.persistence.StoredProcedureQuery;
import jakarta.persistence.Table;
import org.hibernate.dialect.Oracle10gDialect;
import org.hibernate.dialect.OracleDialect;
import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess;
import org.hibernate.engine.jdbc.spi.JdbcServices;

View File

@ -20,7 +20,6 @@ import jakarta.persistence.StoredProcedureParameter;
import jakarta.persistence.StoredProcedureQuery;
import jakarta.persistence.Table;
import org.hibernate.dialect.Oracle10gDialect;
import org.hibernate.dialect.OracleDialect;
import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess;
import org.hibernate.engine.jdbc.spi.JdbcServices;

View File

@ -20,6 +20,7 @@ import java.util.regex.Pattern;
import org.hibernate.Session;
import org.hibernate.dialect.MariaDBDialect;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.dialect.TiDBDialect;
import org.hibernate.procedure.ProcedureCall;
import org.hibernate.query.procedure.ProcedureParameter;
import org.hibernate.result.Output;
@ -30,6 +31,7 @@ import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.hibernate.testing.orm.junit.SkipForDialect;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -48,6 +50,7 @@ import static org.junit.jupiter.api.Assertions.fail;
*/
@RequiresDialect(value = MySQLDialect.class, matchSubTypes = false, majorVersion = 5)
@RequiresDialect(value = MariaDBDialect.class, majorVersion = 5)
@SkipForDialect(dialectClass = TiDBDialect.class, reason = "TiDB doesn't support stored procedures")
@Jpa(
annotatedClasses = {
Person.class,

View File

@ -7,46 +7,48 @@
package org.hibernate.orm.test.query;
import java.util.List;
import org.hibernate.annotations.Nationalized;
import org.hibernate.dialect.SQLServerDialect;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import org.hibernate.annotations.Nationalized;
import org.hibernate.dialect.SQLServer2008Dialect;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* @author Vlad Mihalcea
*/
@TestForIssue(jiraKey = "HHH-10183")
@RequiresDialect(SQLServer2008Dialect.class)
public class SQLServer2008NationalizedScalarQueryTest extends BaseCoreFunctionalTestCase {
@RequiresDialect(value = SQLServerDialect.class, majorVersion = 10)
@DomainModel(
annotatedClasses = SQLServer2008NationalizedScalarQueryTest.User.class
)
@SessionFactory
public class SQLServer2008NationalizedScalarQueryTest {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] { User.class };
}
@Test
public void testScalarResult() {
public void testScalarResult(SessionFactoryScope scope) {
User user1 = new User( 1, "Chris" );
User user2 = new User( 2, "Steve" );
doInHibernate( this::sessionFactory, session -> {
scope.inTransaction( session -> {
session.save( user1 );
session.save( user2 );
} );
doInHibernate( this::sessionFactory, session -> {
scope.inTransaction( session -> {
List<Object[]> users = session.createNativeQuery(
"select * from users" ).getResultList();
assertEquals( 2, users.size() );

View File

@ -9,7 +9,7 @@ package org.hibernate.orm.test.envers.integration.basic;
import java.util.Arrays;
import jakarta.persistence.EntityManager;
import org.hibernate.dialect.Oracle8iDialect;
import org.hibernate.dialect.OracleDialect;
import org.hibernate.orm.test.envers.BaseEnversJPAFunctionalTestCase;
import org.hibernate.orm.test.envers.Priority;
import org.hibernate.orm.test.envers.entities.StrTestEntity;
@ -23,7 +23,7 @@ import junit.framework.Assert;
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
@TestForIssue(jiraKey = "HHH-7246")
@RequiresDialect(Oracle8iDialect.class)
@RequiresDialect(OracleDialect.class)
public class EmptyStringTest extends BaseEnversJPAFunctionalTestCase {
private Integer emptyId = null;
private Integer nullId = null;

View File

@ -14,7 +14,7 @@ import org.hibernate.boot.model.relational.AuxiliaryDatabaseObject;
import org.hibernate.boot.model.relational.Database;
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
import org.hibernate.boot.model.relational.internal.SqlStringGenerationContextImpl;
import org.hibernate.dialect.Oracle8iDialect;
import org.hibernate.dialect.OracleDialect;
import org.hibernate.envers.enhanced.OrderedSequenceGenerator;
import org.hibernate.envers.enhanced.SequenceIdRevisionEntity;
import org.hibernate.orm.test.envers.BaseEnversFunctionalTestCase;
@ -31,7 +31,7 @@ import org.junit.Test;
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
*/
@TestForIssue(jiraKey = "HHH-7669")
@RequiresDialect(Oracle8iDialect.class)
@RequiresDialect(OracleDialect.class)
public class MonotonicRevisionNumberTest extends BaseEnversFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {