声明createEntityManagerFactory的最佳方法

时间:2017-08-13 00:02:57

标签: java spring jpa spring-boot

我实际上在使用JPA的springboot项目上工作。我正在寻找更好的实施,目前它有效,但我的印象是它不是最好的方式

    @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();

...}

2 个答案:

答案 0 :(得分:2)

Spring JPA有助于减少配置数据存储库所需的样板代码。

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

  1. 创建您的域名
  2. 实体

    @Entity
    public class DataCenter {
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private String id;
    
        private String name;
    
        private String location;
    
       .......
    
    }
    
    1. 创建接口存储库以处理与您的实体相关的数据库操作。
    2. 存储库

      public interface DataCenterRepository extends JpaRepository<DataCenter,String> {}
      
      1. Repository自动连接到您的控制器,此示例适用于标准控制器,但它也适用于RestController
      2. 控制器

        @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";
            }
        

        如果您被迫@Autowired EntityManagerFactory,那么

        @Autowired
        private EntityManagerFactory entityManagerFactory;
        

答案 1 :(得分:1)

在spring boot中创建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数据库。此配置将自动创建DataSourceEntityManagerFactoryJpaTransactionManager bean,从而简化数据库连接。您还可以使用以下代码访问entityManager对象:

   @PersistenceContext
   private EntityManager entityManager;

有用的链接: