hibernate table not found for H2 database - java

I am miagrating my database from MySql to H2 and I keep getting the error message
org.h2.jdbc.JdbcSQLException: Table "DEVICE" not found
Everything was mapped correctly and worked with MySql. I only changed the context.xml file to work with H2 and added a dependency for H2 in the Pom.xml file.
context.xml file:
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:~/dataStore2"/>
<property name="username" value="" />
<property name="password" value="" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.entities" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
<prop key="format_sql">true</prop>
<prop key="use_sql_comments">true</prop>
</props>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
The Device class:
package com.entities;
#Entity
#Table(name="DEVICE")
public class Device {
...
}

You're missing
<prop key="hibernate.hbm2ddl.auto">create</prop>
in
<property name="hibernateProperties">
to force Hibernate to create schema based on entity classes if it is missing. You also need to change dialect from MySQL to H2:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.entities" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
<prop key="format_sql">true</prop>
<prop key="use_sql_comments">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
Reference: Hibernate, Chapter 3. Configuration, Table 3.7. Miscellaneous Properties

Related

Error using Hibernate with H2 in database

I'm working with Hibernate. How can I configure my applicationContext.xml to have an H2 in-memory databaseorg.hibernate.dialect.H2Dialect dont work
Spring configuration
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:tcp://localhost/~/test" />
<property name="username" value="sa" />
<property name="password " value="" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.emusicstore</value>
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
You are not connecting to an in-memory database. Your JDBC URL is for network connection to localhost:
jdbc:h2:tcp://localhost/~/test
To use the in-memory H2 the URL must look like this, containing mem:
jdbc:h2:mem:testdb
In the manual, see the section on In-Memory Database
Use embedded db datasource (with dbcp connection pool) instead of drivermanager datasource.
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:db/sql/create-db.sql" />
<jdbc:script location="classpath:db/sql/insert-data.sql" />
</jdbc:embedded-database>
<bean id="dbcpDataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:hsqldb:mem:dataSource" />
<property name="username" value="username" />
<property name="password" value="password" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
<property name="dataSource" ref="dbcpDataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.emusicstore</value>
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

Tomcat Data Source Losing Connection With DB

I need a solution about tomcat datasource configuration.
I have 2 nginx front of my tomcats.These are feeding my app, these are Webservice.
Tomcat machines and database are in different ip blog so its going firewall in each request.
When i started my tomcats everythigs well.But for example after 10 hour our after 20 hour later everythings going bad.That tomcats can not set connection.All service down.I share my datasource file and hope you find some solution or some other suggestions other than datasource file.
http://www.springframework.org/schema/beans ----
http://www.springframework.org/schema/context/spring-context.xsd">
<property name="connectionProperties">
<props merge="default">
<prop key="v$session.program">ws_${server}</prop>
</props>
</property>
<property name="connectionCacheProperties">
<props merge="default">
<prop key="MinLimit">1</prop>
<prop key="MaxLimit">120</prop>
<prop key="InitialLimit">5</prop>
<prop key="MaxStatementsLimit">50</prop>
<prop key="ConnectionWaitTimeout">30</prop>
<prop key="InactivityTimeout">600</prop>
<prop key="AbandonedConnectionTimeout">180</prop>
<prop key="PropertyCheckInterval">300</prop>
<prop key="ValidateConnection">true</prop>
<prop key="TimeToLiveTimeout">600</prop>
</props>
</property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="nativeJdbcExtractor">
<bean class="org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor" />
</property>
<property name="dataSource" ref="dataSource" />
</bean>
<bean class="com.ws.oracle.pool.JndiExporter">
<property name="bean" ref="dataSource" />
<property name="jndiName" value="dsName" />
</bean>

Spring hibernate transactional removing entity

I have Spring MVC application divided on layers. One of them is Service and here's one of the methods
#Transactional
public boolean deleteProject(long id){
Project project = projectDAO.read(id);
taskDAO.deleteAllByProject(project);
projectDAO.delete(project);
return true;
}
It deletes tasks, but project remains in the db (with no error or exception). I didnt use #Transactional in repository classes.
Here's my Spring config
<?xml version="1.0" encoding="UTF-8"?>
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/automate" />
<property name="username" value="postgres" />
<property name="password" value="" />
</bean>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="packagesToScan" value="ru.jeak.keep" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="connection.pool_size">3</prop>
<prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
<prop key="hibernate.default_schema">test</prop>
</props>
</property>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
<bean id="userHibernateDAO" class="ru.jeak.keep.repository.hibernate.UserHibernateDAO" />
<bean id="projectHibernateDAO" class="ru.jeak.keep.repository.hibernate.ProjectHibernateDAO" />
<bean id="taskHibernateDAO" class="ru.jeak.keep.repository.hibernate.TaskHibernateDAO" />
UPDATE
I added persistence #Transactional to DAO class and it works now. But Im still confused why Spring did not do the rollback of deleting tasks after failure of deleting project

Oracle Datasource invalid or stale object exception

I have a connection cache pool created and occasionally I get the Invalid stale connection object error. When I refresh the page and do the same operation it is working fine, means the exception is gone.
This is my config for the connection pool,
<bean id="myDataSource" class="oracle.jdbc.pool.OracleDataSource"
destroy-method="close">
<property name="connectionCachingEnabled" value="true" />
<property name="URL" value="${jdbcUrl-myapp}" />
<property name="user" value="${jdbcUsername-myapp}" />
<property name="password" value="${jdbcPassword-myapp}" />
<property name="connectionProperties">
<value>
oracle.jdbc.timezoneAsRegion=false
</value>
</property>
<property name="connectionCacheProperties">
<props merge="default">
<prop key="MinLimit">0</prop>
<prop key="MaxLimit">100</prop>
<prop key="InitialLimit">1</prop>
<prop key="ConnectionWaitTimeout">600</prop>
<prop key="InactivityTimeout">300</prop>
<prop key="ValidateConnection">true</prop>
</props>
</property>
</bean>
Basically you need to get ride of invalid/stale connection.
Try following set of properties:
<bean id="myDataSource" class="oracle.jdbc.pool.OracleDataSource"
destroy-method="close">
<property name="connectionCachingEnabled" value="true" />
<property name="URL" value="${jdbcUrl-myapp}" />
<property name="user" value="${jdbcUsername-myapp}" />
<property name="password" value="${jdbcPassword-myapp}" />
<property name="connectionProperties">
<value>
oracle.jdbc.timezoneAsRegion=false
</value>
</property>
<property name="connectionCacheProperties">
<props merge="default">
<prop key="MinLimit">0</prop>
<prop key="MaxLimit">100</prop>
<prop key="InitialLimit">1</prop>
<prop key="ConnectionWaitTimeout">600</prop>
<prop key="InactivityTimeout">300</prop>
<prop key="ValidateConnection">true</prop>
<prop key="testOnBorrow">true</prop>
<prop key="testOnReturn">true</prop>
<prop key="testWhileIdle">true</prop>
<prop key="validationQuery">select 1 from dual</prop>
<prop key="removeAbandoned">true</prop>
</props>
</property>
</bean>

Multiple LocalSessionFactoryBean does not work

I have multiple data sources and the dynamic session mapping used to work fine when I used "org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" Now I have upgraded my application to hibernate 4 and started using "org.springframework.orm.hibernate4.LocalSessionFactoryBean". For some reason hibernate is not able to map the tables from my second data source.
Here are my configs.
<bean id="DataSource1" class="org.apache.commons.dbcp.BasicDataSource"
autowire="byName" destroy-method="close">
<property name="driverClassName" value="$api{d1.jdbc.driver}" />
<property name="url" value="$api{d1.jdbc.url}" />
<property name="username" value="$api{d1.jdbc.username}" />
<property name="password" value="$api{d1.jdbc.password}" />
<property name="maxActive" value="$api{dbcp.maxActive}" />
<property name="maxWait" value="$api{dbcp.maxWait}" />
<property name="minIdle" value="$api{dbcp.minIdle}" />
<property name="maxIdle" value="$api{dbcp.maxIdle}" />
<property name="validationQuery" value="$api{dbcp.validationQuery}" />
<property name="testOnBorrow" value="true" />
</bean>
<bean id="d1SessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
autowire="byName">
<property name="dataSource" ref="DataSource1" />
<property name="annotatedClasses">
<list>
<value>com.class1</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.transaction.flush_before_completion">true</prop>
<prop key="hibernate.transaction.auto_close_session">true</prop>
</props>
</property>
</bean>
<bean id="d1Dao" class="com.DaoImpl"
autowire="byName">
<property name="sessionFactory" ref="d1SessionFactory"></property>
</bean>
And the second session definition is
<bean id="DataSource2" class="org.apache.commons.dbcp.BasicDataSource"
autowire="byName" destroy-method="close">
<property name="driverClassName" value="$api{d1.jdbc.driver}" />
<property name="url" value="$api{d1.jdbc.url}" />
<property name="username" value="$api{d2.jdbc.username}" />
<property name="password" value="$api{d2.jdbc.password}" />
<property name="maxActive" value="$api{dbcp.maxActive}" />
<property name="maxWait" value="$api{dbcp.maxWait}" />
<property name="minIdle" value="$api{dbcp.minIdle}" />
<property name="maxIdle" value="$api{dbcp.maxIdle}" />
<property name="validationQuery" value="$api{dbcp.validationQuery}" />
<property name="testOnBorrow" value="true" />
</bean>
<bean id="d2SessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
autowire="byName">
<property name="dataSource" ref="DataSource2" />
<property name="annotatedClasses">
<list>
<value>com.class2</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.transaction.flush_before_completion">true</prop>
<prop key="hibernate.transaction.auto_close_session">true</prop>
</props>
</property>
</bean>
<bean id="d2Dao" class="com.DaoImpl"
autowire="byName">
<property name="sessionFactory" ref="d2SessionFactory"></property>
</bean>
At run time when I do
Session session = this.sessionFactory.openSession() ;
The session always corresponds to d1SessionFactory even when dao is d2Dao.
Not sure what am I doing wrong here.
The same config worked fine when I was using "AnnotationSessionFactoryBean"
Help is appreciated.
The error I get is as follows
org.hibernate.hql.internal.ast.QuerySyntaxException: Class2 is not mapped [FROM Class2 WHERE user_id = :value0 AND active = :value1] at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180) at
org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110) at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:93) at
org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:326) at
org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3252) at
org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3141) at
org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:694) at
org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:550) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:287) at
org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:235) at
org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:248) at
org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183) at
org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136) at
org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:101) at
org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80) at
org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:119) at
org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:215) at
org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:193) at
org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1649)

Categories

Resources