Raible's Wiki

Raible Designs
Wiki Home
News
Recent Changes

AppFuse

Homepage
  - Korean
  - Chinese
  - Italian
  - Japanese

QuickStart Guide
  - Chinese
  - French
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish
  - Japanese

User Guide
  - Korean
  - Chinese

Tutorials
  - Chinese
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish

FAQ
  - Korean

Latest Downloads

Other Applications

Struts Resume
Security Example
Struts Menu

Set your name in
UserPreferences


Referenced by
Articles
Articles_pt
CreateDAO_es
CreateDAO_pt
CreateDAO_sp
CreateDAOiBATIS
CreateManager
CreateManager_es
CreateManager_ko
CreateManager_zh
...and 3 more




JSPWiki v2.2.33

[RSS]


Hide Menu

CreateDAO


Difference between version 33 and version 32:

At line 24 changed 1 line.
* [5] Notify Spring/Hibernate that this object exists in src/dao/**/hibernate/applicationContext-hibernate.xml
* [5] Configure Spring for the Person object and PersonDao
At line 316 changed 1 line.
Let's start by creating a PersonDaoHibernate class that implements the methods in PersonDao and uses Hibernate to get/save/delete the Person object. To do this, create a new class in src/dao/**/persistence and name it PersonDAOHibernate.java. It should extend Spring's [HibernateDaoSupport|http://www.springframework.org/docs/api/org/springframework/orm/hibernate/support/HibernateDaoSupport.html] and implement PersonDAO.
Let's start by creating a PersonDaoHibernate class that implements the methods in PersonDao and uses Hibernate to get/save/delete the Person object. To do this, create a new class in src/dao/**/persistence and name it PersonDAOHibernate.java. It should extend Spring's [HibernateDaoSupport|http://www.springframework.org/docs/api/org/springframework/orm/hibernate/support/HibernateDaoSupport.html] and implement PersonDAO. ''Javadocs eliminated for brevity.''
At line 320 changed 1 line.
package org.appfuse.persistence;
package org.appfuse.persistence.hibernate;
At line 322 removed 2 lines.
import net.sf.hibernate.Session;
At line 327 changed 3 lines.
public class PersonDaoHibernate extends BaseDaoHibernate implements PersonDao {
private Log log = LogFactory.getLog(PersonDaoHibernate.class);
import org.appfuse.model.Person;
import org.appfuse.persistence.DAOException;
import org.appfuse.persistence.PersonDao;
At line 331 changed 3 lines.
public PersonDaoHibernate(Object conn) {
this.ses = (Session) conn;
}
import org.springframework.orm.hibernate.support.HibernateDaoSupport;
At line 331 added 4 lines.
public class PersonDaoHibernate extends HibernateDaoSupport implements PersonDao {
private Log log = LogFactory.getLog(PersonDaoHibernate.class);
At line 336 changed 1 line.
return (Person) retrieveObject(ses, Person.class, id);
Person t = (Person) getHibernateTemplate().get(Person.class, id);
if (t == null) {
throw new DAOException("No Person found with Id '" + id + "'");
}
return t;
At line 339 changed 2 lines.
public void savePerson(Person person) throws DAOException {
saveObject(ses, person);
public void savePerson(Person template) throws DAOException {
getHibernateTemplate().saveOrUpdate(template);
At line 343 changed 2 lines.
public void removePerson(Person person) throws DAOException {
removeObject(ses, person.getId(), person);
public void removePerson(Person template) throws DAOException {
getHibernateTemplate().delete(template);
At line 349 changed 1 line.
Now, if you try to run "ant test-ejb -Dtestcase=PersonDao", you will get an error that no Persister was found for the Person object, which brings us to the final step.
Now, if you try to run "ant test-ejb -Dtestcase=PersonDao", you will get the same error. We need to configure Spring so it knows that PersonDaoHibernate is the implementation of PersonDAO, and we also need to tell it about the Person object.
At line 351 changed 2 lines.
!!Notify Hibernate that this object exists [#5]
We have to tell Hibernate that this Person is persistable. For the JUnit tests, this is done in the src/ejb/**/persistence/ServiceLocator.java class. Simply add this class to the list of existing classes in the following code block:
!!Configure Spring for the Person object and PersonDao [#5]
At line 359 added 2 lines.
First, we need to tell Spring where the Hibernate mapping file is located. To do this, open src/dao/**/hibernate/applicationContext-hibernate.xml and add {{Person.hbm.xml}} to the following code block.
At line 356 changed 4 lines.
sf = new Configuration().addClass(Person.class)
.addClass(Role.class)
.addClass(UserRole.class)
.addClass(User.class).buildSessionFactory();
<property name="mappingResources">
<list>
<value>org/appfuse/model/Person.hbm.xml</value>
<value>org/appfuse/model/Role.hbm.xml</value>
<value>org/appfuse/model/User.hbm.xml</value>
<value>org/appfuse/model/UserCookie.hbm.xml</value>
<value>org/appfuse/model/UserRole.hbm.xml</value>
</list>
</property>
At line 362 changed 1 line.
The JUnit tests use the database.properties file to initialize a Hibernate database connection, whereas Tomcat uses JNDI. Therefore, we must edit a different file for Tomcat: web/WEB-INF/classes/hibernate.cfg.xml. Add a new line for the Person.hbm.xml file in the following XML fragment:
Now we need to add some XML to this file to bind PersonDaoHibernate to PersonDao. To do this, add the following at the bottom of the file:
At line 366 changed 4 lines.
<mapping resource="org/appfuse/persistence/Person.hbm.xml"/>
<mapping resource="org/appfuse/persistence/Role.hbm.xml"/>
<mapping resource="org/appfuse/persistence/User.hbm.xml"/>
<mapping resource="org/appfuse/persistence/UserRole.hbm.xml"/>
<!-- PersonDao: Hibernate implementation -->
<bean id="personDao" class="org.appfuse.persistence.hibernate.PersonDaoHibernate">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>

Back to CreateDAO, or to the Page History.