Cannot create JDBC driver of class 'org.hsqldb.jdbcDriver' for connect URL ' jdbc:hsqldb:db/database' - spring

hi my project is Hsql embedded with spring+hibernate
my diectory like
project name
..src
....domain
....dao
....service
....main
..applicationContext.xml
..db/database
my database name is database
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd">
<!-- Component scan to find all Spring components -->
<context:component-scan base-package="com.habitz.librarymanagement" />
<!-- Data Source -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value=" jdbc:hsqldb:db/database" />
<property name="username" value="sa" />
<property name="password" value="" />
<property name="initialSize" value="1" />
<property name="maxActive" value="5" />
<property name="poolPreparedStatements" value="true" />
<property name="maxOpenPreparedStatements" value="10" />
</bean>
<!-- Hibernate Session Factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- Hibernate configuration -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
</props>
</property>
<!-- The packages that contain our Hibernate model classes -->
<property name="packagesToScan">
<list>
<value>com.habitz.librarymanagement.domain</value>
</list>
</property>
<!--
<property name="cacheRegionFactory">
<bean id="cacheRegionFactory" class="org.hibernate.cache.impl.NoCachingRegionFactory" />
</property>
<property name="eventListeners">
<map></map>
</property>
-->
</bean>
<!-- Hibernate transaction management -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
when i run through main class it gives me
Cannot create JDBC driver of class 'org.hsqldb.jdbcDriver' for connect URL ' jdbc:hsqldb:db/database'
canot find suitable drivers...
but hsqldb.jar in claspath..!!
someone know please help..!!

Regarding the previous answer to clarify confusion.
Both driver classnames are valid.
If you look at org.hsqldb.jdbcDriver it's a very simple class
/* Copyright (c) 2001-2011, The HSQL Development Group
package org.hsqldb;
import org.hsqldb.jdbc.JDBCDriver;
public class jdbcDriver extends JDBCDriver {}
All it does is extend org.hsqldb.jdbc.JDBCDriver and is therefore just an alias to it.

I'm not an expert of XML but it seems to me the name of the driver class is wrong.
Using plain Java code, I pass "org.hsqldb.jdbc.JDBCDriver" as the driver's classname.

Related

While creating session factory object using spring it is showing error"

Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring-mvc-crud-demo-servlet.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException. Below is my XML File
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- Add support for component scanning -->
<context:component-scan base-package="com.code.springdemo" />
<!-- Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>
<!-- Define Spring MVC view resolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- Step 1: Define Database DataSource / connection pool -->
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false&serverTimezone=UTC" />
<property name="user" value="springstudent" />
<property name="password" value="springstudent" />
<!-- these are connection pool properties for C3P0 -->
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="20" />
<property name="maxIdleTime" value="30000" />
</bean>
<!-- Step 2: Setup Hibernate session factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="packagesToScan" value="com.code.springdemo.entity" />
<property name="hibernateProperties">
<props>
<prop
key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<!-- Step 3: Setup Hibernate transaction manager -->
<bean id="myTransactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- Step 4: Enable configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="myTransactionManager" />
Got an answer :
Use --add-modules to add module in classpath.
As Java has not yet removed from module from java 9. Java has only deprecated and does not add javax.xml.bind module on classpath by default.
So if we want to add javax.xml.bind on classpath we can add using following command.
--add-modules java.xml.bind
RUN AS->RUN Configuration->Arguments (In Eclipse)

Error with Spring5+hibernate+jpa+hikaricp xml configuration

Please help. The below is my XML configuration file. My application runs properly. But it says no database selected when call database.
The exception looks like below:
2018-03-24 13:44:17 DEBUG SqlExceptionHelper:139 - could not extract ResultSet [n/a]
java.sql.SQLException: No database selected
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2794)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2322)
at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeQuery(ProxyPreparedStatement.java:52)
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="app.sphi" />
<mvc:resources mapping="/res/**" location="/WEB-INF/res/" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<property name="poolName" value="springHikariCP" />
<property name="connectionTestQuery" value="SELECT 1" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/testdb?useUnicode=true" />
<property name="dataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<!-- Create default configuration for Hibernate -->
<bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
</bean>
<!-- Configure the entity manager factory bean -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
<!-- Set JPA properties -->
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
</props>
</property>
<!-- Set base package of your entities -->
<property name="packagesToScan" value="app.sphi.model" />
<!-- Set share cache mode -->
<property name="sharedCacheMode" value="ENABLE_SELECTIVE" />
<!-- Set validation mode -->
<property name="validationMode" value="NONE" />
</bean>
<!-- Configure the transaction manager bean -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- Enable annotation driven transaction management -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Configure Spring Data JPA and set the base package of the repository interfaces -->
<jpa:repositories base-package="app.sphi.repo"
transaction-manager-ref="transactionManager"
entity-manager-factory-ref="entityManagerFactory" />
</beans>
I will be pleased for any suggestion.
Do not configure both jdbcUrl and dataSourceClassName, pick one style or the other. In this case, I suggest you stick with jdbcUrl.

Spring JPA configuration issue

I have gone through almost all the posts here regarding Spring JPA configuration. Basically I want to implement JPA Pagination in my project and for that I need spring JPA support. But when I include the spring-data-jpa.jar in my project I get the following exception.
Caused by: java.lang.IllegalArgumentException: JBAS011470: Persistence unitName was not specified and there are 2 persistence unit definitions in application deployment deployment "CPCardApp.ear". Either change the application deployment to have only one persistence unit definition or specify the unitName for each reference to a persistence unit.
at org.jboss.as.jpa.container.PersistenceUnitSearch.ambiguousPUError(PersistenceUnitSearch.java:172)
at org.jboss.as.jpa.container.PersistenceUnitSearch.findWithinDeployment(PersistenceUnitSearch.java:152)
at org.jboss.as.jpa.container.PersistenceUnitSearch.findWithinApplication(PersistenceUnitSearch.java:86)
at org.jboss.as.jpa.container.PersistenceUnitSearch.findPersistenceUnitSupplier(PersistenceUnitSearch.java:76)
at org.jboss.as.jpa.container.PersistenceUnitSearch.resolvePersistenceUnitSupplier(PersistenceUnitSearch.java:63)
at org.jboss.as.jpa.processor.JPAAnnotationParseProcessor.getPersistenceUnit(JPAAnnotationParseProcessor.java:357)
at org.jboss.as.jpa.processor.JPAAnnotationParseProcessor.getBindingSource(JPAAnnotationParseProcessor.java:288)
at org.jboss.as.jpa.processor.JPAAnnotationParseProcessor.processMethod(JPAAnnotationParseProcessor.java:201)
at org.jboss.as.jpa.processor.JPAAnnotationParseProcessor.processPersistenceAnnotations(JPAAnnotationParseProcessor.java:138)
at org.jboss.as.jpa.processor.JPAAnnotationParseProcessor.deploy(JPAAnnotationParseProcessor.java:95)
at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:116) [jboss-as-server-7.1.3.Final-redhat-4.jar:7.1.3.Final-redhat-4]
... 5 more
persistence.xml (Note: I have 2 persistence units in my xml and Spring doesn't support that)
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="data01">
<jta-data-source>java:/MySqlDataDS</jta-data-source>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.cache.use_second_level_cache"
value="false" />
<property name="hibernate.id.new_generator_mappings" value="false" />
</properties>
</persistence-unit>
<persistence-unit name="cpaudit">
<jta-data-source>java:/MySqlAuditDS</jta-data-source>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.cache.use_second_level_cache"
value="false" />
<property name="hibernate.id.new_generator_mappings" value="false" />
</properties>
</persistence-unit>
</persistence>
You can have 2 persistence units. There are problems with your config (combo of using JBoss + Spring + JPA (Hibernate) + JTA). The proper configuration will depend on your version of JBoss and Spring.
No matter what you will need to have an xml file to define your JPA config with two entity managers.
Try something like this in a application-jpa-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:transaction="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<beans>
<bean id="entityManagerFactorData01" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaProperties">
<map>
<entry key="hibernate.cache.use_second_level_cache" value="false"/>
<entry key="hibernate.id.new_generator_mappings" value="false"/>
<entry key="hibernate.transaction.jta.platform" value-ref="jtaPlatform"/>
</map>
</property>
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
<property name="jtaDataSource">
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}"/>
<property name="password" value="${database.data01.password}"/>
<property name="url" value="${database.data01.url}"/>
<property name="username" value="${database.data01.user}"/>
</bean>
</property>
<property name="packagesToScan" value="com.app.entites.data01"/>
</bean>
<bean id="entityManagerFactoryCpaudit"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaProperties">
<map>
<entry key="hibernate.cache.use_second_level_cache" value="false"/>
<entry key="hibernate.id.new_generator_mappings" value="false"/>
<entry key="hibernate.transaction.jta.platform" value-ref="jtaPlatform"/>
</map>
</property>
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
<property name="jtaDataSource">
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}"/>
<property name="password" value="${database.cpaudit.password}"/>
<property name="url" value="${database.cpaudit.url}"/>
<property name="username" value="${database.cpaudit.user}"/>
</bean>
</property>
<property name="packagesToScan" value="com.app.entites.cpaudit"/>
</bean>
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" id="jpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
</bean>
<context:annotation-config/>
<jpa:repositories base-package="com.app.repo.data01" entity-manager-factory-ref="entityManagerFactorData01"/>
<jpa:repositories base-package="com.app.repo.cpaudit" entity-manager-factory-ref="entityManagerFactoryCpaudit"/>
<!-- ************ JpaTransactionManager *********** -->
<!--
This will depend on your JBoss and Spring version and your application.
You will need to work this out.
Getting your transactionManager correct could be difficult.
See the following project for more ideas:
https://github.com/manish-in-java/spring-data-jta/blob/master/src/main/resources/springContext.xml
-->
<transaction:annotation-driven/>
<bean class="java.lang.String" id="jtaPlatform">
<constructor-arg value="org.hibernate.engine.transaction.jta.platform.internal.JBossStandAloneJtaPlatform"/>
</bean>
<bean class="org.springframework.transaction.jta.JtaTransactionManager" id="transactionManager">
<property name="transactionManager">
<bean class="com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionManagerImple"/>
</property>
<property name="userTransaction">
<bean class="com.arjuna.ats.jta.UserTransaction" factory-method="userTransaction"/>
</property>
</bean>
<!-- ************ JpaTransactionManager *********** -->
</beans>
</beans>

Spring Entity Manager Factory using JNDI

I am getting confused on how to get the entity Manager Factory in spring spring-datasource.xml using
I did the following :
spring-datasource.xml file:
<tx:jta-transaction-manager id="transactionManager" />
<tx:annotation-driven mode="proxy"
transaction-manager="transactionManager" />
<jee:jndi-lookup id="entityManagerFactory" jndi-name="jdbc/mysqldatasource" />
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="ebankingUnit"
transaction-type="JTA">
<jta-data-source>jdbc/mysqldatasource</jta-data-source>
<class>com.datamodel.Product</class>
<properties>
<property name="eclipselink.logging.level" value="INFO" />
<property name="eclipselink.query-results-cache.expiry"
value="5000" />
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="hibernate.hbm2ddl.auto" value="none" />
</properties>
</persistence-unit>
but I am getting thid
which version of Spring are you using? You don't even need persistence.xml in the latest versions.
Here is the configuration using Spring 3.x, for oracle.
<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- FactoryBean that creates the EntityManagerFactory -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="ORACLE" />
<property name="showSql" value="false" />
</bean>
</property>
<property name="packagesToScan" value="blah.com..domain" />
<property name="jpaProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/myoracledb}" />
</bean>
Now define myoracledb jndi resource in your web.xml
<Resource name="myoracledb" auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
maxActive="50"
url="your db url"
username="dbuser" password="dbpwd" />
Regards

Inject common attribute from spring configuration file to every test class

I have small clarification regarding the spring injection. I'm writing the test for services. Every Test class has a common attribute called "tenantId". Can I inject that attribute through spring configuration file.I don't want to add every test class to the spring configuration file, Is there a way to that?
#ContextConfiguration(locations = {"classpath*:applicationContext-service-test.xml"})
public class ApplicationServiceTest extends AbstractTestNGSpringContextTests {
#Autowired
TenantBasedSessionFactory tenantBasedSessionFactory;
#Autowired
private ApplicationService applicationService;
private String tenantId = "tenantId"; // I want this to inject from applicationContext-service-test.xml
private Session session;
}
Spring configuration file.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- Use #Transaction annotations for managing transactions -->
<tx:annotation-driven/>
<!-- Activates scanning of #Autowired -->
<context:annotation-config/>
<!-- Activates scanning of #Service and #Repository -->
<context:component-scan base-package="lk.gov.elg"/>
<!-- JDBC property file -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<!--<value>classpath:test.jdbc.properties</value>-->
<value>jdbc.properties</value>
</list>
</property>
</bean>
<!--<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"-->
<!--p:driverClassName="org.hsqldb.jdbcDriver" p:url="jdbc:hsqldb:mem:test"-->
<!--p:username="sa" p:password="" />-->
<!-- create database connection pool -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!--<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">-->
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="100"/>
<property name="maxWait" value="1000"/>
<property name="poolPreparedStatements" value="true"/>
<property name="defaultAutoCommit" value="true"/>
</bean>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="lk.gov.elg.orm.model"/>
<property name="hibernateProperties">
<value>
hibernate.dialect=${hibernate.dialect.test}
hibernate.hbm2ddl.auto=update
hibernate.cache.use_second_level_cache=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="tenantBasedSessionFactory" class="lk.gov.elg.orm.dao.impl.TenantBasedSessionFactoryImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
</beans>
Thank you in advance
Cheers
You can create a common superclass for all your tests to inject there your tenantId. With this, you will only have to inject only once and tenandId will be available to all your desired classes.

Resources