0

I am able to create the table successfully through hibernate in Java application but not able to see the same in the H2 database. May be I need another pair of eyes to look into this problem?

Here is my config file.

<hibernate-configuration>
    <session-factory>
        <property name="connection.driver_class">org.h2.Driver</property>
        <property name="connection.url">jdbc:h2:~/hibernateDB;DB_CLOSE_ON_EXIT=TRUE;MVCC=FALSE;MV_STORE=FALSE;FILE_LOCK=NO</property>        
        <property name="connection.username">vijayasunil</property>
        <property name="connection.password"></property>        
        <property name="connection.pool_size">1</property>       
        <property name="dialect">org.hibernate.dialect.H2Dialect</property>
        <property name="current_session_context_class">thread</property>
        <property name="cache.use_query_cache">false</property>
        <property name="cache.use_second_level_cache">false</property>
        <property name="cache.use_structured_entries">true</property>
        <property name="cache.region.factory_class">org.hibernate.cache.internal</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">create</property>
        <mapping class="com.learning.hibernate.dto.UserDetails"/>
    </session-factory>
</hibernate-configuration>

The H2 Console looks like as below

H2 Console The Java Hibernate console is saying as table is created.

Mar 22, 2016 11:15:04 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.1.0.Final}
Mar 22, 2016 11:15:04 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Mar 22, 2016 11:15:04 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Mar 22, 2016 11:15:05 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Mar 22, 2016 11:15:05 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Mar 22, 2016 11:15:05 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [org.h2.Driver] at URL [jdbc:h2:~/hibernateDB;DB_CLOSE_ON_EXIT=TRUE;MVCC=FALSE;MV_STORE=FALSE;FILE_LOCK=NO]
Mar 22, 2016 11:15:05 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=vijayasunil, password=****}
Mar 22, 2016 11:15:05 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Mar 22, 2016 11:15:05 AM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
Mar 22, 2016 11:15:05 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
Hibernate: drop table TEST_DETAILS if exists
Hibernate: create table TEST_DETAILS (userId integer not null, userName varchar(255), primary key (userId))
Mar 22, 2016 11:15:06 AM org.hibernate.tool.schema.internal.SchemaCreatorImpl applyImportSources
INFO: HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@2250b9f2'
*********
SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=ExecutableList{size=0} updates=ExecutableList{size=0} deletions=ExecutableList{size=0} orphanRemovals=ExecutableList{size=0} collectionCreations=ExecutableList{size=0} collectionRemovals=ExecutableList{size=0} collectionUpdates=ExecutableList{size=0} collectionQueuedOps=ExecutableList{size=0} unresolvedInsertDependencies=null])
*********
Hibernate: insert into TEST_DETAILS (userName, userId) values (?, ?)

Entity class in Java code as below

@Entity(name="TEST_DETAILS")
public class UserDetails {
    @Id
    private int userId;
    private String userName;

    public int getUserId() {
        return userId;
    }
    public void setUserId(int userId) {
        this.userId = userId;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }   
}

Updating the Database by using the below piece of code

public class HibernateTest {

    public static void main(String[] args) {

        UserDetails user = new UserDetails();
        user.setUserId(1);
        user.setUserName("Test User");

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

}
Sunny
  • 103
  • 8
  • You are using h2 database in embedded mode. The embedded mode database can be opened from one connection only. Here is the documentation. Are you accessing the database when the database is already connected from other JVM. – Sandeep Sukhija Mar 22 '16 at 06:56
  • @Sandeep Suhkhija, I am accessing the database once after completing the execution of the Java Program. Am I answered your question? I am opening the H2 database in default mode. Do I need to open the database in another mode? May I know the details? – Sunny Mar 22 '16 at 07:32
  • What is the script to create table, insert record? And where do you run these statements? – VinhNT Mar 22 '16 at 09:40
  • Ok, these links http://stackoverflow.com/questions/17803718/view-content-of-embedded-h2-database-started-by-spring and http://stackoverflow.com/questions/7309359/view-content-of-h2-or-hsqldb-in-memory-database might help. – Sandeep Sukhija Mar 22 '16 at 14:27

0 Answers0