【问题标题】:Can't configure JPA with Intellij无法使用 Intellij 配置 JPA
【发布时间】:2017-11-19 10:10:09
【问题描述】:

我正在尝试配置一个简单的 JPA 项目,但我非常卡住。

我有一个工作数据源 (MySQL) 和带有多个表的 GlassFish。

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

  1. 使用带有 Hibernate Provider 的 Java EE Persistance 创建新项目。

  2. 导入具有 Student 表的 DB。

  3. 使用 Intellij IDE 生成实体类。

  4. 从 Maven 导入 Hibernate-entitymanager 库。

  5. 导入 MySQL 连接器。

  6. 尝试运行一个简单的 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)

【问题讨论】:

  • 你的文件结构是什么样的?你在哪里有persistence.xml

标签: java mysql hibernate jpa intellij-idea


【解决方案1】:

你定义了

<persistence-unit name="NewPersistenceUnit">

尝试拨打Persistence.createEntityManagerFactory("persistenceUnit");

NewPersistenceUnit 更改为persistenceUnit

【讨论】:

  • 你完全是我的英雄。如果我还没有结婚,我会请你做我的伴郎。另一个问题 - 它仅适用于 org.hibernate:hibernate-entitymanager:4.1.5.Final 并在最新版本 org.hibernate:hibernate-entitymanager:5.10.Final 下崩溃你知道为什么吗?
  • 不幸的是我也结婚了))哈哈。崩溃时有什么异常?顺便说一下,可能的原因请看这里stackoverflow.com/questions/39410183/…
猜你喜欢
  • 2019-10-03
  • 2020-04-10
  • 2013-12-26
  • 2015-05-23
  • 1970-01-01
  • 2019-12-24
  • 2019-03-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多