2017-08-13 40 views
0

我实际上在JPA的springboot项目上工作。我在寻找一个更好的实施,目前它的工作原理,但我的印象中,这是不是最好的办法声明createEntityManagerFactory的最佳方法

@RestController 
public class inscription { 

EntityManagerFactory objFactory = Persistence.createEntityManagerFactory("com.myapplication_jar_0.0.1-SNAPSHOTPU"); 

UserJpaController userCtrl = new UserJpaController(objFactory); 
SerialsJpaController licenseCtrl = new SerialsJpaController(objFactory); 


    @CrossOrigin(origins = CURRENT_IP) 
    @RequestMapping(value = "/createaccount", method = RequestMethod.GET) 
    public CreatAccountResponseTemplate createAccount(
      @RequestParam(value = "login") String login, 
      @RequestParam(value = "password") String password, 
     ) 
    { 
     EntityManager manager = objFactory.createEntityManager(); 

     CreatAccountResponseTemplate responseTemplate = new CreatAccountResponseTemplate(); 

...} 
+1

“最好的” 是主观的。本网站不是根据常见问题的主观问题 –

回答

2

春天JPA有助于减少被需要,以配置您的数据存储库的样板代码。

也许使用EntityManagerFactory作为您的RestController的成员可能是不必要的依赖项。这是另一种选择:

  1. 创建域

实体

@Entity 
public class DataCenter { 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private String id; 

    private String name; 

    private String location; 

    ....... 

} 
  • 创建界面库,以处理数据库操作相关到您的实体。
  • public interface DataCenterRepository extends JpaRepository<DataCenter,String> {} 
    
  • 自动装配Autowired的Repository到控制器,该示例中为标准控制器,但它也完全适用于一个RestController太。
  • 的控制器

    @Controller 
    @RequestMapping("/datacenters") 
    public class DataCenterController { 
    
        private final DataCenterRepository dataCentersRepository; 
    
        @Autowired 
        public DataCenterController(DataCenterRepository dataCentersRepository){ 
         this.dataCentersRepository=dataCentersRepository; 
        } 
    
    @RequestMapping(value = "/save", method=RequestMethod.POST) 
    public ModelAndView save(@RequestParam(value="name") String datacenterName, 
              @RequestParam(value="age") String datacenterLocation, ModelAndView modelAndView) { 
        DataCenter dataCenter = new DataCenter(datacenterName, datacenterLocation); 
        dataCentersRepository.save(dataCenter); 
        modelAndView.addObject("datacenter", dataCenter); 
        modelAndView.setViewName("success"); 
        return modelAndView; 
    } 
    
        @RequestMapping(value="/all", method=RequestMethod.GET) 
        public String getAll(Model model){ 
         model.addAttribute("datacenters", dataCentersRepository.findAll()); 
         return "datacenters"; 
        } 
    

    如果你被迫@AutowiredEntityManagerFactory那么就

    @Autowired 
    private EntityManagerFactory entityManagerFactory; 
    
    1

    最好的办法春季启动创建EntityManagerFactory是写在下面配置application.properties文件。

    spring.jpa.show-sql=false 
    spring.jpa.properties.hibernate.format_sql=false 
    spring.jpa.hibernate.ddl-auto=update 
    spring.jpa.database-platform = org.hibernate.dialect.PostgreSQLDialect 
    
    spring.datasource.url=jdbc:postgresql://localhost:5432/testdb 
    spring.datasource.username=postgres 
    spring.datasource.password=root 
    spring.datasource.driver-class-name=org.postgresql.Driver 
    

    以上配置使用postgreSQL数据库。此配置将自动创建DataSource,EntityManagerFactoryJpaTransactionManager bean,从而简化数据库连接。你也可以用下面的代码访问entityManager对象:

    @PersistenceContext 
        private EntityManager entityManager; 
    

    相关链接: