Java Reference
In-Depth Information
public class Main {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext(" beans-hibernate.xml");
CourseDao courseDao = (CourseDao) context.getBean("courseDao");
The preceding factory bean creates a session factory by loading the Hibernate configuration
file, which includes the database settings (either JDBC connection properties or a data source's JNDI
name). Now, suppose that you have a data source defined in the Spring IoC container. If you want
to use this data source for your session factory, you can inject it into the dataSource property of
LocalSessionFactoryBean . The data source specified in this property will override the database settings
in the Hibernate configuration file. If this is set, the Hibernate settings should not define a connection
provider to avoid meaningless double configuration.
<beans ...>
<bean id="dataSource"
<property name="driverClassName"
value="org.apache.derby.jdbc.ClientDriver" />
<property name="url"
value="jdbc: derby://localhost:1527/course; create=true" />
<property name="username" value="app" />
<property name="password" value="app" />
<bean id="sessionFactory"
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
Or you can even ignore the Hibernate configuration file by merging all the configurations into
LocalSessionFactoryBean . For example, you can specify the locations of the XML mapping files in
the mappingResources property and other Hibernate properties such as the database dialect in the
hibernateProperties property.
<bean id="sessionFactory"
<property name="dataSource" ref="dataSource" />
<property name=" mappingResources">
Search WWH ::

Custom Search