I currently use spring for depency injection. Hibernate uses a postgres dialect for the normal run, but I want to use HSQL for the DBUnitils Databank-Testing.
My Spring-Configuration contains this:
<!-- Hibernate session factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="use_outer_join">${hibernate.use_outer_join}</prop>
<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
<prop key="hibernate.cache.provider_class">${hibernate.cache.provider}</prop>
<prop key="hibernate.connection.pool_size">10</prop>
<prop key="hibernate.jdbc.batch_size">1000</prop>
<prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>de.dbruhn.relations.model.Relation</value>
<value>de.dbruhn.relations.model.RelationData</value>
<value>de.dbruhn.relations.model.RObject</value>
<value>de.dbruhn.relations.model.Type</value>
</list>
</property>
<property name="schemaUpdate" value="${hibernate.schemaUpdate}"/>
</bean>
The fields get replaced by maven resource-filtering.
The Spring-Configruation for DBUnitils contains this:
<bean id="dataSource" class="org.unitils.database.UnitilsDataSourceFactoryBean"/>
</beans>
and so overrides the dataSource from my run configuration with the UnitilsDataSource.
The Problem: I cant run the Tests using Postgres-Dialect against the HSQL-Test-Database because some commands dont work.
The only solution which came to my mind: Switching the resource-filter in maven, but I have to do this by hand (by provining a "-P TEST" on every maven call). So isn't there a possibilty to override the hibernate.dialect?
Thanks!
You normally don't need to specify the dialect at all, Hibernate will figure it out by looking at the underlying datasource. You only need to specify the dialect if you want to force Hibernate to use a specific one.
In your case, you should just be able to remove the dialect property completely, and it should work in postgres and hsql without config modification.
You should possibly look at using the PropertyPlaceHolderConfigurer in spring to change the config. That way you need only supply a different config file for the different environments, the spring xml stays the same.
And you can load the config file like this.
Related
After upgrading Hibernate version from 4.3.9.Final to 5.0.2.Final, Tomcat Server start-up time has increased.
After debugging, I realized that hibernate takes too much time in adding mapping locations (*.hbm.xml files) in its metadata sources.
I have added mapping location in session factory using following code, and in my project there are around 1000 hbm.xml files.
<bean id="baseSessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.jdbc.fetch_size">300</prop>
<prop key="net.sf.ehcache.configurationResourceName">/ehcache.xml</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
</props>
</property>
<property name="dataSource" ref="dataSource" />
<property name="mappingLocations">
<list>
<value>classpath*:com/*/**/*.hbm.xml</value>
</list>
</property>
</bean>
While starting tomcat server, I debugged and fournd that in method
org.springframework.orm.hibernate5.LocalSessionFactoryBean.afterPropertiesSet
following for loop takes too much time to add all mapping locations in metadata sources object of configuration class.
if (this.mappingLocations != null) {
// Register given Hibernate mapping definitions, contained in resource files.
for (Resource resource : this.mappingLocations) {
sfb.addInputStream(resource.getInputStream());
}
}
Is there any solution to improve performance here? Anyone noticed such issue after upgrading to Hibernate-5?
I have a spring app which do minimal operations with DB.
And I have a requirment that my application should run under the absence of DB(or when db is down).Below is my datasource configuration.
<bean id="dt31DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" depends-on="systemPropertyInitializer"
p:driverClass="${dt31.driver_class}"
p:jdbcUrl="${dt31.url}"
p:user="${dt31.username}"
p:password="${dt31.password}"
p:idleConnectionTestPeriod="1000"
p:maxPoolSize="4"
p:minPoolSize="2"
p:maxIdleTime="2000"
p:unreturnedConnectionTimeout="600"
p:contextClassLoaderSource="library"
p:privilegeSpawnedThreads="true"
p:initialize=false
/>
<bean id="dt31SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dt31DataSource"/>
<property name="packagesToScan" value="com.t22.dt31"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
${dt31.dialect}
</prop>
<prop key="hibernate.show_sql">
false
</prop>
<prop key="hibernate.hbm2ddl.auto">
update
</prop>
</props>
</property>
</bean>
I found a page in google,saying to use "initialize: false" in spring data source configuration.But I am using "ComboPooledDataSource" datasource which does not have this property.Is there any other way to achieve this ?
The application service layer can operate with two DAO layers (file system and database), so when the DB is down you catch the connection acquire exception and switch to the file system instead.
Having two sources of truth is going to make it difficult to preserve consistency across two different data sources, especially if one resource is not available.
When both resources are available you can use XADisk and Bitronix for XADataSource and JTA transaction management.
It seems to be a common problem but I am not able to resolve it.
my Spring 3.1.1 configuration is
<bean id="sessionFactoryEditSolution" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="editSolutions-pool"/>
<property name="mappingResources">
<list>
<value>/editsolutions.hibernate.cfg.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</prop>
<prop key="net.sf.ehcache.configurationResourceName">/ehcache.xml</prop>
<prop key="hibernate.max_fetch_depth">6</prop>
<prop key="hibernate.default_schema">dbo</prop>
</props>
</property>
</bean>
And this how I am trying to get the configuration object
Configuration editSolutionsConfiguration = `(Configuration)AppContext.getBean("&sessionFactoryEditSolution");`
Initially I have Hibernate 3 for my application but to integrate it with Spring 3.1.1 I have upgraded it to Hibernate 4 .That's why I have to keep hibernate3.jar in my lib folder to support few hibernate 3 specific code lines.
A couple of notes:
you can't work with two versions of hibernate on the classpath. Your old code should be updated
&x returns the factory bean, it doesn't return the produced object. Even without the ampersand, this would return the SessionFactory, rather than Configuration.
with spring you don't need Configuration actually, it is handled behind the scene
we are working with hibernate 3 and spring 3.1. 3.1.1 might be different, but either keep the lower versions of both, or upgrade both (including code)
I have two tables Parent and Child.
Child table references to Parent table.
In application configuration of spring dao. Can't I give configuration of only child?
like below.
<bean id="ChildDAOSpringTarget" class="project.dao.spring.ChildDAOSpring">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="ChildDAO"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager" />
</property>
<property name="target">
<ref local="ChildDAOSpringTarget" />
</property>
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly
</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly
</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly
</prop>
<prop key="store*">PROPAGATION_REQUIRED</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
Do I have to have add this kind of configuration and classes for Parent even?
I don't config relation between parent and child table in applicationContext.xml but I set the relation in modelXXX.hbm.xml file or you can use hibernate tools to generate model link
In hibernate, parent-child relationship has been defined in hbm file or it will be annotation based. And in application configuration file, you have to define database connection's properties, session factory, bean mapping etc.
Yep as mentioned above... Check here for one to many tutorial which is likely what you are trying to do.
http://www.mkyong.com/hibernate/hibernate-one-to-many-relationship-example/
The child becomes a collection of the parent and is accessed as such.
Spring doesn't need to reference the child - instead you reference the children through the parent like so:
parent.getChildren().[...]
I am trying to set a property value using spring.
<bean id="velocityPropsBean" class="com.test.CustomProperties" abstract="false" singleton="true" lazy-init="false" autowire="default" dependency-check="default">
<property name="properties">
<props>
<prop key="resource.loader">file</prop>
<prop key="file.resource.loader.cache">true</prop>
<prop key="file.resource.loader.class">org.apache.velocity.runtime.resource.loader.FileResourceLoader</prop>
<prop key="file.resource.loader.path">NEED TO INSERT VALUE AT STARTUP</prop>
</props>
</property>
</bean>
<bean id="velocityResourcePath" class="java.lang.String" factory-bean="velocityHelper" factory-method="getLoaderPath"/>
Now what i need to do is insert the result from getLoaderPath into file.resource.loader.path. The value of getLoaderPath changes so it has to be loaded at server startup.
Any thoughts how i can inset the velocityResourcePath value to the property?
Using Spring 3, you can skip the middle stage, and invoke the factory directly using SpringEL:
<prop key="file.resource.loader.path">#{ velocityHelper.loaderPath }</prop>
or perhaps
<prop key="file.resource.loader.path">#{ velocityHelper.getLoaderPath() }</prop>
This would let you remove the velocityResourcePath bean.
Below code might help you.
<import resource="classpath:/DaoContext.xml"/>
<bean id="ClientMasterDao" class="dao.hibernate.impl.ClientMasterImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="ClientMasterServices" class="client.ClientMasterServices">
<property name="clientDao" ref="ClientMasterDao"/>
</bean>