Here is my hibernate config
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:mem:/localhost/testDB"/>
<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 name="packagesToScan" value="sample.model"/>
<property name="annotatedPackages">
<list>
<value>sample.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"> org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
I included this dependency
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
</dependency>
Here is the exception
Caused by: java.lang.IllegalStateException: Expected method not found: java.lang.NoSuchMethodException: org.hibernate.cfg.Configuration.addAnnotatedClass(java.lang.Class)
at org.springframework.util.ClassUtils.getMethod(ClassUtils.java:627)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.<clinit>(LocalSessionFactoryBuilder.java:79)
... 39 more
What am I missing here?
It looks like your hibernate annotations jar file is the incorrect version.
You should already have hibernate-core as a dependency. If so you can comment out your hibernate-annotations dependency in your POM file.
The solution was to use hibernate 3 and Adding this
<prop key="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</prop>
So the final config is
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="datasource" />
<property name="packagesToScan" value="sample.model"/>
<property name="annotatedPackages">
<list>
<value>sample.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"> org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</prop>
</props>
</property>
</bean>
Related
We have an application where we have implemented Abstract Routing Data source feature in dealing multiple data bases of same type mysql (jdbc:mysql://127.0.0.1:3306/test, jdbc:mysql://127.0.0.1:3306/test2).
Now we are going with different database which is DB2. so i was unable to find any samples for this scenario using abstract routing datasource.
Can anyone give any directions ?
This is my code block:
i have defined 3 datasources itemDataSource, custDataSource, orderDb2DataSource in dao.xml .
how to configure the second session factory to the the transaction manager ?
<bean id="dataSource" class="com.test.DatabaseRoutingDataSource">
<property name="targetDataSources">
<map key-type="com.test.dao.DatabaseType">
<entry key="ITEM" value-ref="itemDataSource"/>
<entry key="CUSTOMER" value-ref="custDataSource"/>
<entry key="ORDER_DB2" value-ref="orderDb2DataSource"/>
</map>
</property>
<property name="defaultTargetDataSource" ref="itemDataSource" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="configLocation">
<value>/WEB-INF/hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySqlDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- Added for DB2 Database -->
<bean id="sessionFactoryDB2" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="configLocation">
<value>/WEB-INF/hibernate.db2.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.DB2Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- Added for DB2 Database -->
<bean id="transactionManager1"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
I want my program to exit if it cannot connect to the database on startup. Currently this connection is setup using the following:
application-context.xml
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${db.driver}</value>
</property>
<property name="url">
<value>${db.url}</value>
</property>
<property name="username">
<value>${db.username}</value>
</property>
<property name="password">
<value>${db.password}</value>
</property>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="packagesToScan" value="com.template" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.ejb.naming_strategy">${naming_strategy}</prop>
<prop key="hibernate.connection.autoReconnect">true</prop>
<prop key="hibernate.connection.autoReconnectForPools">true</prop>
<prop key="hibernate.connection.check-valid-connection-sql">SELECT 1</prop>
<prop key="hibernate.connection.failOverReadOnly">false</prop>
<prop key="hibernate.connection.maxReconnects">${maxreconnects}</prop>
<prop key="hibernate.connection.initialTimeout">${reconnect.interval}</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
Repository:
#Repository
public interface Repository extends CrudRepository<Object, String> {
}
It's not obvious where I should place code to catch the runtime exceptions created by the connection failure. Are there any other settings I can use to exit if the database doesn't exist.
Usually, spring will stop, if it could not create a bean at startup. If DB connection fails, then it will stop automatically anyway. Do you want to catch that exception, and do something before exit ?
I am trying to create spring+hibernate application, after added hibernate configurations I am getting org.springframework.beans.factory.BeanCreationException while starting tomcat server:
Error creating bean with name 'dataSource' defined in ServletContext
resource [/WEB-INF/hibernate-config.xml]: Initialization of bean
failed; nested exception is java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotationUtils.getAnnotation(Ljava/lang/reflect/AnnotatedElement;Ljava/lang/Class;)Ljava/lang/annotation/Annotation;
hibernate-config.xml
<context:component-scan base-package="com.test.common">
</context:component-scan>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.test.common.model.CompanyTypes</value>
<value>com.test.common.model.Employee</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<!-- <prop key="hibernate.current_session_context_class">thread</prop> -->
<!--prop key="current_session_context_class">thread</prop-->
<!-- <prop key="hibernate.connection.release_mode">on_close</prop> -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/testDB" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
jars included:
please guide, thanks in advance..
I am trying to config two sessionFactories using spring. My config looks similar to the one listed here
Here's my config.xml
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url">
<value>${hibernate.connection.url}</value>
</property>
<property name="driverClassName">
<value>${hibernate.connection.driver_class}</value>
</property>
<property name="username">
<value>${hibernate.connection.username}</value>
</property>
<property name="password">
<value>${hibernate.connection.password}</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="mappingResources">
<list>
...Mappings
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
${hibernate.dialect}
</prop>
<prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.default_batch_fetch_size">${hibernate.default_batch_fetch_size}</prop>
<prop key="hibernate.c3p0.min_size">${hibernate.c3p0.min_size}</prop>
<prop key="hibernate.c3p0.max_size">${hibernate.c3p0.max_size}</prop>
<prop key="hibernate.c3p0.timeout">${hibernate.c3p0.timeout}</prop>
<prop key="hibernate.c3p0.max_statements">${hibernate.c3p0.max_statements}</prop>
<prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
</props>
</property>
</bean>
<bean id="dataSource2"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url">
<value>${hibernate.connection.mirror_url}</value>
</property>
<property name="driverClassName">
<value>${hibernate.connection.driver_class}</value>
</property>
<property name="username">
<value>${hibernate.connection.mirror_username}</value>
</property>
<property name="password">
<value>${hibernate.connection.mirror_password}</value>
</property>
</bean>
<bean id="sessionFactory2"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource2" />
</property>
<property name="mappingResources">
<list>
...Mappings
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
${hibernate.dialect}
</prop>
<prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.default_batch_fetch_size">${hibernate.default_batch_fetch_size}</prop>
<prop key="hibernate.c3p0.min_size">${hibernate.c3p0.min_size}</prop>
<prop key="hibernate.c3p0.max_size">${hibernate.c3p0.max_size}</prop>
<prop key="hibernate.c3p0.timeout">${hibernate.c3p0.timeout}</prop>
<prop key="hibernate.c3p0.max_statements">${hibernate.c3p0.max_statements}</prop>
<prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
</props>
</property>
</bean>
Then each dao gets a different sessionFactory assigned
<bean id="productDao"
class="test.dao.ProductDaoHibernate">
<property name="sessionFactory"><ref bean="sessionFactory" /></property>
</bean>
<bean id="currencyDao"
class="test.dao.CurrencyDaoHibernate">
<property name="sessionFactory"><ref bean="sessionFactory2" /></property>
</bean>
This config gets loaded when its added to the context
web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/test-data.xml /WEB-INF/classes/test-services.xml ... </param-value>
</context-param>
The problem shows whenever I start the server each sessionFactory built, but at the end of the second one this shows up:
[INFO] [org.springframework.beans.factory.support.DefaultListableBeanFactory]:? - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory#97aaa6: defining beans [... Many elements...]; root of factory hierarchy
[INFO] [org.springframework.orm.hibernate3.LocalSessionFactoryBean]:? - Closing Hibernate SessionFactory
[INFO] [org.hibernate.impl.SessionFactoryImpl]:? - closing
[INFO] [org.springframework.orm.hibernate3.LocalSessionFactoryBean]:? - Closing Hibernate SessionFactory
[INFO] [org.hibernate.impl.SessionFactoryImpl]:? - closing
Any help, or lead would be appreciated, if you need more info please ask
spring4.2 & hibernate5.1.5
#Bean
public LocalSessionFactoryBean aaaSessionFactory() {
LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
localSessionFactoryBean.setDataSource(smsDataSource());
localSessionFactoryBean.setHibernateProperties(getHibernateProperties());
localSessionFactoryBean.setPackagesToScan(new String[]{"com.xxx.pojo"});
localSessionFactoryBean.setPhysicalNamingStrategy(new CustomNamingStrategy());
localSessionFactoryBean.setEntityInterceptor(new DynamicTableInterceptor());
return localSessionFactoryBean;
}
#Bean
public LocalSessionFactoryBean bbbSessionFactoryMultiple() {
LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
localSessionFactoryBean.setDataSource(smsDataSource());
localSessionFactoryBean.setHibernateProperties(getHibernateProperties());
localSessionFactoryBean.setPackagesToScan(new String[]{"com.xxx.pojo"});
localSessionFactoryBean.setPhysicalNamingStrategy(new CustomNamingStrategy());
localSessionFactoryBean.setEntityInterceptor(new DynamicTableInterceptor());
return localSessionFactoryBean;
}
public class xxx extends HibernateDaoSupport{***
#Autowired
public void anyMethodName(#Qualifier("aaaSessionFactory") SessionFactory sessionFactory) {
setSessionFactory(sessionFactory);
}
Actually this is supposed to be a comment, but don't have enough reputation (requires 50 points)
It seems you are trying to create 2 different bean id's which are of exactly same configuration. One way is to figure out whether 2 different session objects pointing to same configuration is required. Other way is to try adding the configurations in different files and load separately. Please add how you r trying to use these configurations in code.
I want to rewrite my sessionFactory bean using the p-namespace. I understand how to reference general objects, but I've never dealt with lists, props, etc. How would I go about writing , and so on?
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>/com/mysite/domain/Object.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
This is the code I have so far in p-namespace:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
p:dataSource-ref="dataSource"
p:mappingResources-list="/com/mysite/domain/Object.hbm.xml"
p:hibernateProperties-props=""/>
You cannot do this directly . You could use util:map though. Try
<util:map id="hibernateConfig" >
<entry key="hibernate.hbm2ddl.auto" value="update" />
<entry key="hibernate.show_sql" value="true" />
<!-- Other properties -->
</util:map>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
p:dataSource-ref="dataSource"
p:mappingResources-list="/com/mysite/domain/Object.hbm.xml"
p:hibernateProperties-ref ="hibernateConfig"/>
Check This for complete help on p:namespace