简体   繁体   English

声明createEntityManagerFactory的最佳方法

[英]best way to declare createEntityManagerFactory

I work actually on a springboot project with JPA. 我实际上在JPA的springboot项目上工作。 I'm looking for a better implementation, currently it works but I have the impression that it is not the best way 我正在寻找一个更好的实现,目前它的工作原理,但我的印象是它不是最好的方式

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

...}

Spring JPA helps to reduce the boilerplate code that is need in order to configure your data repository. Spring JPA有助于减少配置数据存储库所需的样板代码。

Maybe using the EntityManagerFactory as a member of your RestController could be a unnecesary dependency. 也许使用EntityManagerFactory作为RestController的成员可能是一个不必要的依赖项。 Here is another alternative: 这是另一种选择:

  1. Create your domain 创建您的域名

The entity 实体

@Entity
public class DataCenter {

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

    private String name;

    private String location;

   .......

}
  1. Creates the interface repository in order to handle the database operations related to your Entity. 创建接口存储库以处理与您的实体相关的数据库操作。

Repository 知识库

public interface DataCenterRepository extends JpaRepository<DataCenter,String> {}
  1. Autowired the Repository to your controller, this example is for a standard controller but it also works perfectly for a RestController too. Repository到控制器,此示例适用于标准控制器,但它也适用于RestController

The Controller 控制器

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

If you are forced to @Autowired your EntityManagerFactory then just 如果你被迫@Autowired你的EntityManagerFactory然后

@Autowired
private EntityManagerFactory entityManagerFactory;

The best way to create EntityManagerFactory in spring boot is to write below configuration in application.properties file. 在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

The above configuration uses postgreSQL database. 以上配置使用postgreSQL数据库。 This configuration will automatically create DataSource , EntityManagerFactory and JpaTransactionManager bean and hence simplify database connectivity. 此配置将自动创建DataSourceEntityManagerFactoryJpaTransactionManager bean,从而简化数据库连接。 Also you can access entityManager object with below code: 您还可以使用以下代码访问entityManager对象:

   @PersistenceContext
   private EntityManager entityManager;

Useful links: 有用的链接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用main方法声明变量的Java最佳方法 - java best way to declare variables with main method Java:声明一叠纸牌的最佳方法 - Java: best way to declare a stack of cards 哪个是在java中声明记录器变量的最佳方法 - Which is the best way to declare logger variable in java 在 Kotlin 中声明常量的最佳方法是什么? - What's the best way to declare a constant in Kotlin? 在 Java 中长时间运行的线程中声明数组的最佳方法 - Best way to declare an array in a long running thread in Java 在Eclipse中声明编译和运行时之间的瞬时依赖关系的最佳方法是什么? - What is the best way to declare transient dependencies between compile and runtime with Eclipse? 在Apache Wink中声明可选命名路径参数的最佳方法是什么 - What is the best way to declare optional named path parameters in Apache Wink 使用Kotlin在Android上的UI组件上声明的最佳方法是什么? - What is the best way to declare on UI component in android with Kotlin? 通过匿名 class 声明新线程的最佳方法是什么? - What's the best way to declare a new thread via an anonymous class? JPA CreateEntityManagerFactory()方法挂起 - JPA CreateEntityManagerFactory() method hangs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM