Approach for a (generic) DDD Repository with JPA/Spring: does it look wrong?

荒凉一梦 提交于 2019-12-03 17:19:34

First of all, it wouldn't work, because Spring can't inject EntityManager into internal object created with new. So, you have to write something like this:

public class OrganizationRepository implements IOrganizationRepository { 

    @PersistenceContext
    public void setEntityManager(EntityManager em) {
        internalRepository.em = em;
    }
    ...
}

Also your read method looks a bit too generic. It misses some important use cases, such as getSigleResult and setFirstResult/setMaxResults.

Personally I prefer the second article apporach, because using composition you'll end up with having EntityManager in OrganizationRepository in order to implement features missed in IInternalGenericRepository.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!