1

I am trying simple hibernate example.But, i'm unable to create database table using hibernate.What could be the problem? Java version1.7.Hibernate 3.6.4 My hibernate.cfg.xml is directly under src directory:

false com.mysql.jdbc.Driver password jdbc:mysql://localhost:3306/hibernatedb root org.hibernate.dialect.MySQLDialect true

    <mapping class="org.koushik.javabrains.dto.UserDetails"></mapping>
</session-factory>

my main class: public class HibernateTest {

public static void main(String[] args)

{

UserDetails user=new UserDetails();

user.setUserId(1);

user.setUsername("First user");

SessionFactory sessionfactory=new Configuration().configure().buildSessionFactory();
Session session=sessionfactory.openSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
}

}

This is the complete trace i get: 202 [main] INFO org.hibernate.annotations.common.Version - Hibernate Commons Annotations 3.2.0.Final 213 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.6.4.Final 215 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found 220 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist 227 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling 331 [main] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml 331 [main] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml 466 [main] INFO org.hibernate.cfg.Configuration - Configured SessionFactory: null 549 [main] INFO org.hibernate.cfg.AnnotationBinder - Binding entity from annotated class: org.koushik.javabrains.dto.UserDetails 618 [main] INFO org.hibernate.cfg.annotations.EntityBinder - Bind entity org.koushik.javabrains.dto.UserDetails on table UserDetails 693 [main] INFO org.hibernate.cfg.Configuration - Hibernate Validator not found: ignoring 703 [main] INFO org.hibernate.cfg.search.HibernateSearchEventListenerRegister - Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled. 715 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Using Hibernate built-in connection pool (not for production use!) 715 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Hibernate connection pool size: 20 715 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - autocommit mode: false 733 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/hibernatedb 733 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - connection properties: {user=root, password=**} 1197 [main] INFO org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.MySQLDialect 1219 [main] INFO org.hibernate.cfg.SettingsFactory - Database -> name : MySQL version : 5.6.12-log major : 5 minor : 6 1219 [main] INFO org.hibernate.cfg.SettingsFactory - Driver -> name : MySQL Connector Java version : mysql-connector-java-5.1.25 ( Revision: ${bzr.revision-id} ) major : 5 minor : 1 1220 [main] INFO org.hibernate.transaction.TransactionFactoryFactory - Using default transaction strategy (direct JDBC transactions) 1222 [main] INFO org.hibernate.transaction.TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended) 1222 [main] INFO org.hibernate.cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled 1222 [main] INFO org.hibernate.cfg.SettingsFactory - Automatic session close at end of transaction: disabled 1223 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC batch size: 15 1223 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC batch updates for versioned data: disabled 1223 [main] INFO org.hibernate.cfg.SettingsFactory - Scrollable result sets: enabled 1223 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC3 getGeneratedKeys(): enabled 1223 [main] INFO org.hibernate.cfg.SettingsFactory - Connection release mode: auto 1224 [main] INFO org.hibernate.cfg.SettingsFactory - Maximum outer join fetch depth: 2 1224 [main] INFO org.hibernate.cfg.SettingsFactory - Default batch fetch size: 1 1225 [main] INFO org.hibernate.cfg.SettingsFactory - Generate SQL with comments: disabled 1225 [main] INFO org.hibernate.cfg.SettingsFactory - Order SQL updates by primary key: disabled 1225 [main] INFO org.hibernate.cfg.SettingsFactory - Order SQL inserts for batching: disabled 1225 [main] INFO org.hibernate.cfg.SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory 1228 [main] INFO org.hibernate.hql.ast.ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory 1228 [main] INFO org.hibernate.cfg.SettingsFactory - Query language substitutions: {} 1228 [main] INFO org.hibernate.cfg.SettingsFactory - JPA-QL strict compliance: disabled 1228 [main] INFO org.hibernate.cfg.SettingsFactory - Second-level cache: enabled 1228 [main] INFO org.hibernate.cfg.SettingsFactory - Query cache: disabled 1228 [main] INFO org.hibernate.cfg.SettingsFactory - Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory 1229 [main] INFO org.hibernate.cfg.SettingsFactory - Optimize cache for minimal puts: disabled 1229 [main] INFO org.hibernate.cfg.SettingsFactory - Structured second-level cache entries: disabled 1237 [main] INFO org.hibernate.cfg.SettingsFactory - Echoing all SQL to stdout 1238 [main] INFO org.hibernate.cfg.SettingsFactory - Statistics: disabled 1238 [main] INFO org.hibernate.cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled 1238 [main] INFO org.hibernate.cfg.SettingsFactory - Default entity-mode: pojo 1238 [main] INFO org.hibernate.cfg.SettingsFactory - Named query checking : enabled 1238 [main] INFO org.hibernate.cfg.SettingsFactory - Check Nullability in Core (should be disabled when Bean Validation is on): enabled 1270 [main] INFO org.hibernate.impl.SessionFactoryImpl - building session factory 1278 [main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [materialized_clob] overrides previous : org.hibernate.type.MaterializedClobType@8247262 1278 [main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [characters_clob] overrides previous : org.hibernate.type.PrimitiveCharacterArrayClobType@10d1841b 1279 [main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [clob] overrides previous : org.hibernate.type.ClobType@2b38113d 1279 [main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [java.sql.Clob] overrides previous : org.hibernate.type.ClobType@2b38113d 1279 [main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [materialized_blob] overrides previous : org.hibernate.type.MaterializedBlobType@6df3d1f5 1279 [main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [blob] overrides previous : org.hibernate.type.BlobType@225d6438 1279 [main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [java.sql.Blob] overrides previous : org.hibernate.type.BlobType@225d6438 1279 [main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [wrapper_materialized_blob] overrides previous : org.hibernate.type.WrappedMaterializedBlobType@215eaa28 1279 [main] INFO org.hibernate.type.BasicTypeRegistry - Type registration [wrapper_characters_clob] overrides previous : org.hibernate.type.CharacterArrayClobType@4df53935 1569 [main] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured Hibernate: insert into UserDetails (username, userId) values (?, ?) 1702 [main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 1146, SQLState: 42S02 1702 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Table 'hibernatedb.userdetails' doesn't exist Exception in thread "main" org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184) at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216) at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383) at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133) at org.koushik.hibernate.HibernateTest.main(HibernateTest.java:25) Caused by: java.sql.BatchUpdateException: Table 'hibernatedb.userdetails' doesn't exist at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2054) at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1467) at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70) at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268) ... 8 more Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'hibernatedb.userdetails' doesn't exist at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) at com.mysql.jdbc.Util.getInstance(Util.java:386) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4187) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4119) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2815) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2458) at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2006) ... 11 more

Please help me solve this issue. Thanks.

1 Answer 1

2

Hibernate reads and writes to tables, but doesn't create them (by default). If you want Hibernate to create your tables, then you have to set the property hibernate.hbm2ddl.auto in your config file to one of the documented values:

hibernate.hbm2ddl.auto Automatically validates or exports schema DDL to the database when the SessionFactory is created. With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly.

e.g. validate | update | create | create-drop

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much JB!! i had copied the config from an online resource.It did not have that field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.