How to get JPA configured with Spring 3?

前端 未结 3 1486
生来不讨喜
生来不讨喜 2020-12-23 12:40

I have been reading spring\'s documentation, but I must say it is a bit confusing, giving several different option on how to configure JPA.

What is the best way, and

3条回答
  •  执笔经年
    2020-12-23 13:13

    Alternatively Spring 3+ and JPA 2.0 can be integrated with the help of dynamic proxies.

    You can find all the documentation and download example here

    In this case interfaces with named JPA queries are used to execute queries. Interfaces are treated as ordinary Spring beans with the help of dynamic proxies. They can be injected (or autowired) into any other beans the same way.

    Also queries can be located in separate orm-mapping.xml files and split up by domain (or at your convenience). That gives a high flexibility and maintainability to persistent layer.

    public interface OrganisationQueries {
    
            @Query(named = "find.organisation.by.role.id")
            public Organisation findOrganisationByRoleId(Long roleId);
    
            @Query(named = "find.all.organisations")
            public List findAllOrganisations();
        }
        public class OrganisationServiceImpl implements OrganisationService {
            @PersistenceContext
            private EntityManager em;
            @Autowired
            private OrganisationQueries organisationQueries;
            @Override
            public Organisation findOrganisationByRoleId(Long roleId) {
                return organisationQueries.findOrganisationByRoleId(roleId);
            }
            @Override
            public List findAllOrganisations() {
                return organisationQueries.findAllOrganisations();
            }
        }
    
       
          
              
          
          
               
           
       
    

提交回复
热议问题