[英]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: 这是另一种选择:
The entity 实体
@Entity
public class DataCenter {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private String id;
private String name;
private String location;
.......
}
Repository 知识库
public interface DataCenterRepository extends JpaRepository<DataCenter,String> {}
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. 此配置将自动创建DataSource
, EntityManagerFactory
和JpaTransactionManager
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.