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"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<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>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
</beans>
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"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name=" mappingResources">
<list>
<value>com/apress/springenterpriserecipes/course/Course.hbm.xml</value>
</list>
</property>
Search WWH ::




Custom Search