当前位置: 首页 > 知识库问答 >
问题:

无法使用Intellij配置JPA

唐炜
2023-03-14

我正在尝试配置一个简单的JPA项目,我被卡住了。

我有一个工作数据源(MySQL)

我做了以下步骤(Intellij生成的所有文件):

>

导入具有学生表的数据库。

使用Intellij IDE生成实体类。

从Maven导入Hibernate-entityManager库。

导入MySQL连接器。

尝试运行一个简单的JPA程序来创建新行,但我得到了一个例外:

没有名为persistenceUnit的EntityManager的持久性提供程序

有什么问题吗?我几乎尝试了这里的每一个教程和每一个问题,但都没有成功!

请救救我!

(有时持久提供程序中的Hibernate是红色的)

坚持:

    <?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">

    <persistence-unit name="NewPersistenceUnit">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>modules.CourseEntity</class>
        <class>modules.StuCouEntity</class>
        <class>modules.StudentsEntity</class>
        <class>modules.TeacherEntity</class>
        <properties>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.connection.password" value="123456"/>
            <property name="hibernate.archive.autodetection" value="class"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hbm2ddl.auto" value="update"/>
        </properties>
    </persistence-unit>
</persistence>

Main.java

import modules.StudentsEntity;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class MainClass
{
    public static void main(String[] args)
    {
        StudentsEntity stu = new StudentsEntity();
        stu.setName("David");

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistenceUnit");
        EntityManager em = emf.createEntityManager();

        em.getTransaction().begin();
        try
        {
            em.persist(stu);
            em.getTransaction().commit();
        }
        catch (Exception ex)
        {
            System.out.println(ex.getMessage());
            ex.printStackTrace();
            em.getTransaction().rollback();
        }
        finally
        {
            em.close();
        }



    }
}

错误:

June 16, 2017 5:47:07 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
June 16, 2017 5:47:07 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.5.Final}
June 16, 2017 5:47:07 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
June 16, 2017 5:47:07 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named persistenceUnit
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:84)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
    at MainClass.main(MainClass.java:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

共有1个答案

阎晔
2023-03-14

你定义了

<persistence-unit name="NewPersistenceUnit">

同时尝试调用持久性。createEntityManagerFactory(“persistenceUnit”)

NewPersistenceUnit更改为persistenceUnit

 类似资料:
  • 我正在试用intellij,并且正在尝试进行一些swing开发。我遇到了一个在eclipse上从未遇到过的问题,我想知道我是否设置错了什么。 下面是我的驱动程序运行的GUI类: } 据我所知,我的run()方法非常简单。但是,在编译时,我收到的错误是: 无论出于什么原因,intellij自动创建的代码没有正确初始化JPanel,这就是它为空的原因。 我尝试在run内部实例化它(panel=new

  • 所以我告诉IDEA它应该使用什么JDK,但它仍然要求我设置一个JDK。这是一个窃听器还是我遗漏了什么?我能应用任何变通方法使一切照常工作吗?

  • 我创建了虚拟项目来弄清楚Cucumber for android是如何工作的,并遵循这里的“教程” https://www.jetbrains.com/idea/help/enable-cucumber-support-in-project.html

  • 问题内容: 似乎log4j试图通过XML解析器解析.json配置文件,但是为什么呢? 我通过命令行参数设置log4j配置文件: 文件内容: 我得到的是: 我的lib文件夹中的Jar: hamcrest-junit-2.0.0.0.jar jackson-annotations-2.6.1.jar Jackson core-2.6.1.jar jackson-databind-2.6.1.jar j

  • 我正在使用IntelliJ IDEA,并且Spring的所有插件都被激活了,但是当我加载我的Maven项目时,我遇到了以下错误: Spring配置检查 找到未映射的Spring配置文件。 请为模块配置/设置Spring刻面 有什么想法阻止它自动配置?

  • 我使用IntelliJ IDEA15,但有一个自动保存的问题。例如,我做了一个新的配置,例如Show the line number。当我关闭IntelliJ并重新打开我的IDE时,它不保存我的配置,并向我显示以下消息: 无法保存设置:无法保存设置。请重新启动IntelliJ IDEA