How to get JPA configured with Spring 3?

前端 未结 3 1494
生来不讨喜
生来不讨喜 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:17

    I use EclipseLink, but configuration must be very similar. Here you have most important parts.

    pom.xml:

        
            org.springframework
            spring-orm
            ${org.springframework-version}
        
        
            org.eclipse.persistence
            eclipselink
            2.0.1 
        
         
            javax.persistence
            javax.persistence
            2.0.0
        
    

    persistence.xml:

        
        
    
        
    
    
    

    applicationContext-dao.xml:

    
        
         
        
        
            
                false
            
        
    
    
    
        
    
    
    

    User.java:

    @Entity
    public class User {
    
        @Id
        @GeneratedValue(strategy = GenerationType.SEQUENCE)
        private Integer id;
    
        private String name;
    
        // Getters and setters
    
    }
    

    UserDao.java:

    @Repository
    public class JpaUserDao implements UserDao {
    
        @PersistenceContext
        private EntityManager em;
    
        @Override
        public Item get(Integer id) {
            return em.find(User.class, id);
        }
    }
    

    UserService.java:

    @Service 
    public class UserServiceImpl implements UserService {
    
        @Autowired
        private UserDao userDao;
    
        @Transactional
        @Override
        public User getUser(Integer id) {
            return userDao.get(id);
        }
    
    }
    

    Hope it helps.

提交回复
热议问题