My favorites | Sign in
Project Logo
                
Search
for
Updated Feb 07, 2009 by kuisong.tong
EntityRepository  
Use repository for accessing domain object

Introduction

In repository pattern, Martin told us how to use Repository to isolate domain object accessing. With generic in Java 5, we can implement a generic repository easyly.

Details

The basic repository provide methods get(identifier), save(entity), delete(entity), loadAll(), deleteAll(Collection). You can extend EntityRepository like

Create Repository with Special Interface

        public interface UserRepository extends EntityRepository<User, String> {
        }

and configure it as a spring bean

	<util:bean id="userRepository" interfaces="cn.muthos.polyforms.sample.repository.UserRepository" />

and use it like below:

        public class UserAction {
            @Autowired
            private UserRepository userRepository;
        }

Create Repository with Implementation

Sometimes you need implement addition repository methods, repository also support it. But you need provide a implmentation class for this, eg.

        public abstract class UserRepositoryImpl implements UserRepository {
            @Autowired
            private UserDao userDao;

            List<User> findByName(String name) {
                 return userDao.findByName(name);
            }
        }

	<util:bean id="userRepository" class="cn.muthos.polyforms.sample.repository.impl.UserRepositoryImpl" />

You just need implement partial repository methods, let the repository implementation be a abstract class.

Notice When you add your implementation to repository, you also can inject other bean (eg. dao, repository) as normal spring bean.



Sign in to add a comment
Hosted by Google Code