2017-08-13 60 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; 
    

    相關鏈接: