Initial SessionFactory creation failed-org-hibernate-MappingException-could-not-instantiate-id-generator
If you get below exception when using hibernate:
Initial SessionFactory creation failed.org.hibernate.MappingException: could not instantiate id generator Exception in thread "main" java.lang.ExceptionInInitializerError at com.javahonk.HibernateUtil.buildSessionFactory(HibernateUtil.java:13) at com.javahonk.HibernateUtil.<clinit>(HibernateUtil.java:6) at com.javahonk.InsertData.main(InsertData.java:12) Caused by: org.hibernate.MappingException: could not instantiate id generator at org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:98) at org.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:152) at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:192) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294) at com.javahonk.HibernateUtil.buildSessionFactory(HibernateUtil.java:10) ... 2 more Caused by: org.hibernate.MappingException: Dialect does not support sequences at org.hibernate.dialect.Dialect.getSequenceNextValString(Dialect.java:595) at org.hibernate.id.SequenceGenerator.configure(SequenceGenerator.java:65) at org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:94) ... 6 more
This happens due to table where id is mentioned as primary key and generator class is not able to generate id automatically. To increment primary key id automatically use below class and this will make primary key increment automatically whenever you save the data to the table.
<id name="id" type="int"> <column name="id" /> <generator class="increment" /> </id>
I am facing the same issue..
Oct 23, 2019 2:03:55 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [oracle.jdbc.driver.OracleDriver] at URL [jdbc:oracle:thin:@127.0.0.1:1521:XE]
Oct 23, 2019 2:03:55 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=system, password=****}
Oct 23, 2019 2:03:55 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Oct 23, 2019 2:03:55 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Oct 23, 2019 2:03:56 AM org.hibernate.dialect.Dialect
INFO: HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
Oct 23, 2019 2:03:56 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PoolState stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:oracle:thin:@127.0.0.1:1521:XE]
Exception in thread “main” org.hibernate.MappingException: Could not instantiate id generator [entity-name=pojo.Students]
at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.createIdentifierGenerator(DefaultIdentifierGeneratorFactory.java:124)
at org.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:342)
at org.hibernate.internal.SessionFactoryImpl.lambda$new$1(SessionFactoryImpl.java:281)
at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(Unknown Source)
at java.util.stream.ReferencePipeline$2$1.accept(Unknown Source)
at java.util.HashMap$ValueSpliterator.forEachRemaining(Unknown Source)
at java.util.stream.AbstractPipeline.copyInto(Unknown Source)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)
at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(Unknown Source)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(Unknown Source)
at java.util.stream.AbstractPipeline.evaluate(Unknown Source)
at java.util.stream.ReferencePipeline.forEach(Unknown Source)
at org.hibernate.internal.SessionFactoryImpl.(SessionFactoryImpl.java:280)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:462)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
at test.Main.main(Main.java:18)
Caused by: org.hibernate.MappingException: Could not interpret id generator strategy [increament]
at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.getIdentifierGeneratorClass(DefaultIdentifierGeneratorFactory.java:144)
at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.createIdentifierGenerator(DefaultIdentifierGeneratorFactory.java:115)
… 16 more
If i am commenting like
<!–
–>
then all seems to working provided that you need to provide the whole id details too..
Oct 23, 2019 2:17:18 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.4.1.Final}
Oct 23, 2019 2:17:18 AM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead. Support for obsolete DTD/XSD namespaces may be removed at any time.
Oct 23, 2019 2:17:18 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager
INFO: HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
Oct 23, 2019 2:17:19 AM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-mapping. Use namespace http://www.hibernate.org/dtd/hibernate-mapping instead. Support for obsolete DTD/XSD namespaces may be removed at any time.
Oct 23, 2019 2:17:19 AM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-mapping. Use namespace http://www.hibernate.org/dtd/hibernate-mapping instead. Support for obsolete DTD/XSD namespaces may be removed at any time.
Oct 23, 2019 2:17:19 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Oct 23, 2019 2:17:19 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [oracle.jdbc.driver.OracleDriver] at URL [jdbc:oracle:thin:@127.0.0.1:1521:XE]
Oct 23, 2019 2:17:19 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=system, password=****}
Oct 23, 2019 2:17:19 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Oct 23, 2019 2:17:19 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Oct 23, 2019 2:17:19 AM org.hibernate.dialect.Dialect
INFO: HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
Hibernate: drop table COURCES cascade constraints
Oct 23, 2019 2:17:20 AM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@59532566] 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.
Hibernate: drop table STUDENTS cascade constraints
Hibernate: drop table STUDENTS_COURSES cascade constraints
Hibernate: create table COURCES (courseId number(10,0) not null, courseName varchar2(255 char), courseDuration number(10,0), primary key (courseId))
Oct 23, 2019 2:17:20 AM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@5c748168] 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.
Hibernate: create table STUDENTS (studentId number(10,0) not null, studentName varchar2(255 char), studentGrp varchar2(255 char), primary key (studentId))
Hibernate: create table STUDENTS_COURSES (studentFK number(10,0) not null, courseFK number(10,0) not null, primary key (courseFK, studentFK))
Hibernate: alter table STUDENTS_COURSES add constraint FKcfd4mpf9c8f5kvovxelgl0d98 foreign key (courseFK) references COURCES
Hibernate: alter table STUDENTS_COURSES add constraint FK7kluktrjtx8bhwg4e3vhl47x7 foreign key (studentFK) references STUDENTS
Oct 23, 2019 2:17:20 AM org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator initiateService
INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
Hibernate: select cources_.courseId, cources_.courseName as courseName2_0_, cources_.courseDuration as courseDuration3_0_ from COURCES cources_ where cources_.courseId=?
Hibernate: select cources_.courseId, cources_.courseName as courseName2_0_, cources_.courseDuration as courseDuration3_0_ from COURCES cources_ where cources_.courseId=?
Hibernate: insert into STUDENTS (studentName, studentGrp, studentId) values (?, ?, ?)
Hibernate: insert into COURCES (courseName, courseDuration, courseId) values (?, ?, ?)
Hibernate: insert into COURCES (courseName, courseDuration, courseId) values (?, ?, ?)
Hibernate: insert into STUDENTS (studentName, studentGrp, studentId) values (?, ?, ?)
Hibernate: insert into STUDENTS (studentName, studentGrp, studentId) values (?, ?, ?)
Hibernate: insert into COURCES (courseName, courseDuration, courseId) values (?, ?, ?)
Hibernate: insert into STUDENTS_COURSES (studentFK, courseFK) values (?, ?)
Hibernate: insert into STUDENTS_COURSES (studentFK, courseFK) values (?, ?)
Hibernate: insert into STUDENTS_COURSES (studentFK, courseFK) values (?, ?)
Hibernate: insert into STUDENTS_COURSES (studentFK, courseFK) values (?, ?)
Oct 23, 2019 2:17:20 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PoolState stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:oracle:thin:@127.0.0.1:1521:XE]