Whenever I execute the Main.java file below I get this following error
"org.hibernate.exception.SQLGrammarException: could not fetch initial value for increment generator"
Main.java:
Code:
public class Main {
public void createResonsibilities(){
SessionFactory sessionfactory = NewHibernateUtil.getSessionFactory();
Transaction transaction = null;
try {
Session session = sessionfactory.openSession();
transaction = session.beginTransaction();
responsibilities responsibilityOne=new responsibilities("Java");
responsibilities responsibilityTwo=new responsibilities("SQL");
responsibilities responsibilityThree=new responsibilities("Oracle");
session.save(responsibilityOne);
session.save(responsibilityTwo);
session.save(responsibilityThree);
transaction.commit();
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
}
}
public static void main(String[] args) {
Main main=new Main();
main.createResonsibilities();
}
My hibernate.cfg.xml has the property <property name="hibernate.hbm2ddl.auto">update</property>
So even though the table does not exist in the database it should get created I assume
This my complete error log
org.hibernate.exception.SQLGrammarException: could not fetch initial value for increment generator
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
Hibernate: select max(responsibilityId) from responsibilities
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.id.IncrementGenerator.initializePreviousValueHolder(IncrementGenerator.java:147)
at org.hibernate.id.IncrementGenerator.generate(IncrementGenerator.java:67)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:713)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:701)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:697)
at contacts.Main.createResonsibilities(Main.java:32)
at contacts.Main.main(Main.java:44)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'revision.responsibilities' doesn't exist
this is my responsibility.hbm.xml:
Code:
<hibernate-mapping>
<class name="contacts.responsibilities" table="responsibilities">
<meta attribute="class-description">
</meta>
<id column="responsibilityId" name="responsibilityId" type="long">
<generator class="increment"/>
</id>
<property column="responsibilityName" name="responsibilityName" type="string"/>
</class>
</hibernate-mapping>