A simple example using a one-to-many relationship in Hibernate with an XML configuration to find, save, and update a record.
A Person
has a one-to-many relationship with Address
.
The HsqldbInitializingDriverManagerDataSource
is used to initialize the test HSQLDB database and
the LocalSessionFactoryBean
is using to configure Hibernate. The mappingLocations property is used to set
a list of Hibernate XML mapping files. The Person DAO is configured using Hibernate's session factory.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="dataSource" class="org.springbyexample.jdbc.datasource.HsqldbInitializingDriverManagerDataSource"> <property name="sqlScriptProcessor"> <bean class="org.springbyexample.jdbc.core.SqlScriptProcessor" p:sqlScripts="classpath:/schema.sql" /> </property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" p:dataSource-ref="dataSource"> <property name="mappingLocations"> <list> <value>classpath:org/springbyexample/orm/hibernate3/bean/Address.hbm.xml</value> <value>classpath:org/springbyexample/orm/hibernate3/bean/Person.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.format_sql=true hibernate.dialect=org.hibernate.dialect.HSQLDialect </value> </property> </bean> </beans>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="classpath:org/springbyexample/orm/hibernate3/shared-context.xml"/> <bean id="personDao" class="org.springbyexample.orm.hibernate3.dao.PersonDaoImpl" p:sessionFactory-ref="sessionFactory" /> </beans>