First: yes, I've seen the docs.
They tell me how to create a util:list, util:set, etc. I get that part.
However, I have a library with an application context that contains a bean (specifically a Hibernate Session Factory bean) with settings I'd like the option of overriding. Several services use this library, not every service needs the same annotated classes.
The session factory bean currently looks something like this:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.jdbc.use_get_generated_keys">true</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
</props>
</property>
<property name="annotatedClasses" >
<list>
<value>com.example.model.Person</value>
<value>com.example.model.Section</value>
</list>
</property>
</bean>
I would like to replace the annotatedClasses property with a list defined like this (in the app context of the service using the library):
<util:list id="serviceSpecificAnnotatedClasses">
<value>com.example.model.Person</value>
<value>com.example.model.Section</value>
<value>com.example.model.Location</value>
</util:list>
Do I simply have to name the util:list "annotatedClasses" and it will be automatically overriden?
No it wont automatically be overriden. You would have to declare the bean and wire it by default.
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.jdbc.use_get_generated_keys">true</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
</props>
</property>
<property name="annotatedClasses" ref="annotatedClasses" />
</bean>
<util:list id="annotatedClasses">
<value>com.example.model.Person</value>
<value>com.example.model.Section</value>
<value>com.example.model.Location</value>
</util:list>
No others can simply override the list annotatedClasses.
But why not simply use a property-placeholder to specify the classes and add a comma delimited list to a properties file?
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.jdbc.use_get_generated_keys">true</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
</props>
</property>
<property name="annotatedClasses" value="${service.annotatedClasses}" />
</bean>
Assuming that each service has its own property file for configuration they simply need to add the value for service.annotatedClasses.
Related
The sessionFactory, can't map package
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.env.recru.domain</value>
<value>com.env.curric.domain</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
</props>
</property>
</bean>
In first package, work, but sencond has a package in one artifactId, call env-10.jar
Project use Spring 4
Hibernate 4
Use maven manage dependency
Link of the Reference
How to map jar in that sessionFactory?
I have legacy hbm.xml configured persistent classes and Annotated persistent classes, so currently we have to specify per which session factory is being used in the DAO bean.
Unfortunately, that means that I'm running into an issue where we have a mix of DAO's that I'd like to use, aren't all associated to a single session factory.
I'd like to combine both into a single session factory bean since we can't just move everything over all at once.
How would I go about doing this?
Note: my current workaround is to Annotate some of the xml configured ones and then create two DAO beans: one for each Session Factory.
HBM.XML:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingLocations">
<value>hbm/path/*.hbm.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
</bean>
Annotated:
<bean id="annotatedSessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="path.batch.persistent"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
</bean>
Thanks M.Deinum and orid:
I was overthinking this.
There was never a need for two session factories.
So I just had to refactor the context file to:
<bean id="annotatedSessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="path.batch.persistent"/>
<property name="mappingLocations">
<value>hbm/path/*.hbm.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
</bean>
I have a web application created with Spring data, which uses JpaRepository (org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean with org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter) after some server uptime I have noticed that heap is growing all the time.
After taking a heap dump it shows that heap contains much more entities than I am currently using.
So my question is how to manage EntityManager to clear entities that are not being used any more?
Here is jpa configuration:
<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" p:database="${jdbc.databaseType}"
p:generateDdl="true" p:showSql="false"/>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
<prop key="hibernate.ejb.naming_strategy">com.example.db.repository.FixedDefaultComponentSafeNamingStrategy</prop>
<prop key="hibernate.connection.CharSet">utf8</prop>
<prop key="hibernate.connection.characterEncoding">utf8</prop>
<prop key="hibernate.connection.useUnicode">true</prop>
</props>
</property>
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager" p:entityManagerFactory-ref="entityManagerFactory">
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
</property>
</bean>
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
Ok, I have this spring hibernate xml configuration.
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>com/abc/def/a.xml</value>
<value>com/abc/def/b.xml</value>
<value>com/abc/def/c.xml</value>
<!--
And so on, about 50 xml for example
How can I separate list value above into 5 file for example?
ex I have h1.xml (or h1.txt) that contain
<value>com/abc/def/a.xml</value>
<value>com/abc/def/b.xml</value>
I have h2.xml (or h2.txt) that contain
<value>com/abc/def/c.xml</value>
<value>com/abc/def/d.xml</value>
so the mappingResources just read from the files (more than 1) than contain all mapping objects
-->
</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.cache.provider_class">org.hibernate.cache.OSCacheProvider</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
</props>
</property>
</bean>
I have commented the question in the xml configuration above.
Thanks
You can just use
<property name="mappingLocations">
<list>
<value>classpath:com/abc/def/*.xml</value>
</list>
</property>