LocalSessionFactoryBean can't map package in jar file Spring 4 - java

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?

Related

Class 'org.springframework.orm.hibernate4.LocalSessionFactoryBean' not found

I dont know why I am having the above error on this line of my code
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
this is the complete code
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:db/hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
What could be wrong?
I had this problem long ago and this was my solution :
Hibernate 4 support was added to Spring Framework in 3.1.
Your pom.xml references Spring Framework 2.5.6 hence
org.springframework.orm.hibernate4.LocalSessionFactoryBean does not
exist. You need to update your pom.xml to reference Spring Framework
3.1 or later.
ref : java.lang.ClassNotFoundException: org.springframework.orm.hibernate4.LocalSessionFactoryBean

Spring Cfg+Hibernate Cfg and Anotations

I want to make a test with HQL but the mapping tables is with Annotations.
Hibernate configuration file is in the /WEB-INF/spring-config-ws.xml :::>
<!-- Activate transaction declarations with annotations -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- Property files application uses -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<!-- JNDI DataSource -->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="${dataSource.jndiName}" />
<property name="lookupOnStartup" value="false"/>
<property name="cache" value="true"/>
<property name="proxyInterface" value="javax.sql.DataSource"/>
</bean>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
<property name="packagesToScan">
<list>
<value>es.sergas.rprof.profesional.domain</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.default_schema">${hibernate.default_schema}</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
</props>
</property>
</bean>
<!-- Transaction manager for Hibernate SessionFactory -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
When I run a list, I get that you do not find the hibernate cfg:
hibernate.cfg.xml not found
I just want to list a mapped class with annotations, but HQL
I feel my level of English is so low. Thank you
Make sure your hibernate.cfg.xml is inside src/main/resources, if the file is not there you need to specify the right location, so put it inside this folder that your problem will be solved.
Note that we don’t have to explicitly mention the mapping or configuration or properties
files, because the Hibernate runtime looks for default filenames, such as hibernate.
cfg.xml or hibernate.properties, in the classpath and loads them. If we have a nondefault
name, make sure you pass that as an argument—like configure("my-hibcfg.
xml"), for example.

How to create a hibernate session bean for both Annotated and hbm.xml configured entities

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>

Using Spring Util Schema to Override Bean Settings In App Context

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.

separate mappingResources from spring hibernate LocalSessionFactoryBean

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>

Categories