Entity ``` @Entity @Table(name = "Person") public class Person { @Id @GeneratedValue private Integer id; private String name; private int age; public Person() { // nothing } public Person(String name, int age) { this.name = name; this.age = age; } 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; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } ``` hibernate.properties ``` hibernate.connection.url jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false ``` Test case: ``` package org.hibernate.bugs; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.Metadata; import org.hibernate.boot.MetadataSources; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.junit.Before; import org.junit.Test; /**
- This template demonstrates how to develop a standalone test case for Hibernate ORM. Although this is perfectly
- acceptable as a reproducer, usage of ORMUnitTestCase is preferred!
*/ public class ORMStandaloneTestCase {
private SessionFactory sf; @Before public void setup() { StandardServiceRegistryBuilder srb = new StandardServiceRegistryBuilder() // Add in any settings that are specific to your test. See resources/hibernate.properties for the defaults. .applySetting( "hibernate.show_sql", "true" ) .applySetting( "hibernate.format_sql", "true" ) .applySetting( "hibernate.hbm2ddl.auto", "update" ); Metadata metadata = new MetadataSources( srb.build() ) // Add your entities here. .addAnnotatedClass( Person.class ) .buildMetadata(); sf = metadata.buildSessionFactory(); } // Add your tests, using standard JUnit. @Test public void test() throws Exception { Person p1 = new Person("John", 35); try (Session session = sf.openSession()) { session.beginTransaction(); session.save(p1); session.getTransaction().commit(); } } } ``` Output: ``` 2019-11-13 12:42:33 INFO Version:46 - HHH000412: Hibernate Core {5.4.8.Final} 2019-11-13 12:42:33 INFO Environment:184 - HHH000205: Loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=org.h2.Driver, javax.persistence.validation.mode=NONE, hibernate.service.allow_crawling=false, hibernate.max_fetch_depth=5, hibernate.dialect=org.hibernate.dialect.H2Dialect, hibernate.format_sql=true, hibernate.cache.region.factory_class=org.hibernate.testing.cache.CachingRegionFactory, hibernate.connection.username=sa, hibernate.cache.region_prefix=hibernate.test, hibernate.connection.url=jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false, hibernate.bytecode.use_reflection_optimizer=false, hibernate.show_sql=false, hibernate.session.events.log=true, hibernate.jdbc.batch_versioned_data=true, hibernate.connection.pool_size=5} 2019-11-13 12:42:33 WARN CachingRegionFactory:48 - org.hibernate.testing.cache.CachingRegionFactory should be only used for testing. 2019-11-13 12:42:33 INFO Version:49 - HCANN000001: Hibernate Commons Annotations {5.1.0.Final} 2019-11-13 12:42:33 WARN pooling:72 - HHH10001002: Using Hibernate built-in connection pool (not for production use!) 2019-11-13 12:42:33 INFO pooling:115 - HHH10001005: using driver [org.h2.Driver] at URL [jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false] 2019-11-13 12:42:33 INFO pooling:124 - HHH10001001: Connection properties: {user=sa} 2019-11-13 12:42:33 INFO pooling:129 - HHH10001003: Autocommit mode: false 2019-11-13 12:42:33 INFO DriverManagerConnectionProviderImpl:239 - HHH000115: Hibernate connection pool size: 5 (min=1) 2019-11-13 12:42:34 INFO Dialect:172 - HHH000400: Using dialect: org.hibernate.dialect.H2Dialect 2019-11-13 12:42:34 ERROR JdbcEnvironmentImpl:420 - Could not fetch the SequenceInformation from the database org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "sequences" not found; SQL statement: select * from INFORMATION_SCHEMA.sequences [42102-200] at org.h2.message.DbException.getJdbcSQLException(DbException.java:453) at org.h2.message.DbException.getJdbcSQLException(DbException.java:429) at org.h2.message.DbException.get(DbException.java:205) at org.h2.message.DbException.get(DbException.java:181) at org.h2.command.Parser.readTableOrView(Parser.java:7628) at org.h2.command.Parser.readTableFilter(Parser.java:1970) at org.h2.command.Parser.parseSelectFromPart(Parser.java:2827) at org.h2.command.Parser.parseSelect(Parser.java:2959) at org.h2.command.Parser.parseQuerySub(Parser.java:2817) at org.h2.command.Parser.parseSelectUnion(Parser.java:2649) at org.h2.command.Parser.parseQuery(Parser.java:2620) at org.h2.command.Parser.parsePrepared(Parser.java:868) at org.h2.command.Parser.parse(Parser.java:843) at org.h2.command.Parser.parse(Parser.java:815) at org.h2.command.Parser.prepareCommand(Parser.java:738) at org.h2.engine.Session.prepareLocal(Session.java:657) at org.h2.engine.Session.prepareCommand(Session.java:595) at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1235) at org.h2.jdbc.JdbcStatement.executeQuery(JdbcStatement.java:78) at org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorLegacyImpl.extractMetadata(SequenceInformationExtractorLegacyImpl.java:42) at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentImpl.sequenceInformationList(JdbcEnvironmentImpl.java:403) at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentImpl.<init>(JdbcEnvironmentImpl.java:268) at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:114) at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:101) at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214) at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.injectServices(DefaultIdentifierGeneratorFactory.java:152) at org.hibernate.service.internal.AbstractServiceRegistryImpl.injectDependencies(AbstractServiceRegistryImpl.java:286) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:243) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214) at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.<init>(InFlightMetadataCollectorImpl.java:175) at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:118) at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83) at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:473) at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:84) at org.hibernate.boot.MetadataSources.buildMetadata(MetadataSources.java:185) at org.hibernate.bugs.ORMStandaloneTestCase.setup(ORMStandaloneTestCase.java:30) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) 2019-11-13 12:42:34 INFO access:47 - HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@6c000e0c] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode. 2019-11-13 12:42:34 WARN SqlExceptionHelper:137 - SQL Error: 42102, SQLState: 42S02 2019-11-13 12:42:34 ERROR SqlExceptionHelper:142 - Table "sequences" not found; SQL statement: select * from INFORMATION_SCHEMA.sequences [42102-200] 2019-11-13 12:42:34 INFO pooling:408 - HHH10001008: Cleaning up connection pool [jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false] ``` |