java - 如何在主类 Java 中使用 DAO 模块?
问题描述
我需要在主类中使用一个类对象,但我遇到了一些问题:
public static void main(String[] args) throws NoElementFoundException {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("fantacalcio_rest");
EntityManager em = emf.createEntityManager();
}
这是我的
持久性.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="fantacalcio_rest">
<jta-data-source>java:/fantacalcio_rest</jta-data-source>
<class>model.Player</class>
<class>model.Module</class>
<class>model.User</class>
<class>model.League</class>
<class>model.FantaPlayer</class>
<class>model.PlayerTransfer</class>
<class>model.Formation</class>
<class>model.PlayerMark</class>
<class>model.FantaPlayerMatch</class>
<class>model.CalendarSerieA</class>
<class>model.FantaPlayerRank</class>
<class>model.RankRow</class>
<properties>
<!-- Properties for Hibernate -->
<property name="hibernate.dialect"
value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.enable_lazy_load_no_trans"
value="true" />
</properties>
</persistence-unit>
</persistence>
但我收到了这个错误:
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
我哪里错了?如何在 Java 类中毫无问题地使用 DAO?
解决方案
推荐阅读
- classification - 从分类报告中解释结果以进行多类分类
- android - 在阅读列表中在移动设备上离线缓存 Three.js 页面
- nlp - 按文本内容过滤数据框
- reactjs - 如何动态增加或减少 MUI Grid 列的大小?
- python - python已安装(可以在cmd中显示)但无法运行py文件?
- momentjs - 为什么 moment.js 返回无效日期
- revit-api - Revit 模型在通过 Revit API 与 BIM360 项目放弃所有权后被锁定
- azure-devops - 我需要哪些访问权限才能代表用户创建对 devops 工作项的订阅?
- jenkins - Jenkins 将任何非零退出代码视为错误
- powershell - PowerShell 将 .EXE 版本与 .MSI 版本进行比较