4. Code Example

Example 3. PersonTemplateDaoImpl

                
@Repository
@Transactional(readOnly = true)
public class PersonTemplateDaoImpl extends JpaDaoSupport implements PersonTemplateDao {
    
    /**
     * Constructor
     */
    @Autowired
    public PersonTemplateDaoImpl(EntityManagerFactory entityManagerFactory) {
        super.setEntityManagerFactory(entityManagerFactory);
    }
    
    /**
     * Find persons.
     */
    @SuppressWarnings("unchecked")
    public Collection<Person> findPersons() {
        return getJpaTemplate().find("from Person");
    }
    
    /**
     * Find persons by last name.
     */
    @SuppressWarnings("unchecked")
    public Collection<Person> findPersonsByLastName(String lastName) {
        return getJpaTemplate().find("from Person p where p.lastName = ?", lastName);
    }
    
    /**
     * Saves person.
     */
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public void save(Person person) {
        getJpaTemplate().merge(person);
    }

}