No Persistence provider for EntityManager named

前端 未结 30 1988
甜味超标
甜味超标 2020-11-22 03:45

I have my persistence.xml with the same name using TopLink under the META-INF directory. Then, I have my code calling it with:

相关标签:
30条回答
  • 2020-11-22 04:37

    After <persistence-unit name="agisdb">, define the persistence provider name:

    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    
    0 讨论(0)
  • 2020-11-22 04:37

    In an OSGi-context, it's necessary to list your persistence units in the bundle's MANIFEST.MF, e.g.

    JPA-PersistenceUnits: my-persistence-unit
    

    Otherwise, the JPA-bundle won't know your bundle contains persistence units.

    See http://wiki.eclipse.org/EclipseLink/Examples/OSGi/Developing_with_EclipseLink_OSGi_in_PDE .

    0 讨论(0)
  • 2020-11-22 04:37

    You have to use the absolute path of the file otherwise this will not work. Then with that path we build the file and pass it to the configuration.

    @Throws(HibernateException::class)
    fun getSessionFactory() : SessionFactory {
        return Configuration()
                    .configure(getFile())
                    .buildSessionFactory()
        }
    
    
    private fun getFile(canonicalName: String): File {
        val absolutePathCurrentModule = System.getProperty("user.dir")
        val pathFromProjectRoot = absolutePathCurrentModule.dropLastWhile { it != '/' }
        val absolutePathFromProjectRoot = "${pathFromProjectRoot}module-name/src/main/resources/$canonicalName"
        println("Absolute Path of secret-hibernate.cfg.xml: $absolutePathFromProjectRoot")
        return File(absolutePathFromProjectRoot)
    }
    

    GL

    Source

    0 讨论(0)
  • 2020-11-22 04:38

    Hibernate 5.2.5
    Jar Files Required in the class path. This is within a required folder of Hibernate 5.2.5 Final release. It can be downloaded from http://hibernate.org/orm/downloads/

    1. antlr-2.7.7
    2. cdi-api-1.1
    3. classmate-1.3.0
    4. dom4j-1.6.1
    5. el-api-2.2
    6. geronimo-jta_1.1_spec-1.1.1
    7. hibernate-commons-annotation-5.0.1.Final
    8. hibernate-core-5.2.5.Final
    9. hibernate-jpa-2.1-api-1.0.0.Final
    10. jandex-2.0.3.Final
    11. javassist-3.20.0-GA
    12. javax.inject-1
    13. jboss-interceptor-api_1.1_spec-1.0.0.Beta1
    14. jboss-logging-3.3.0.Final
    15. jsr250-api-1.0

    Create an xml file "persistence.xml" in

    YourProject/src/META-INF/persistence.xml

    persistence.xml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <persistence 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"
             version="2.1">
    
    <persistence-unit name="sample">
    
        <class>org.pramod.data.object.UserDetail</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/hibernate_project"/>
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.connection.password" value="root"/>
            <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="false"/>
    
            <property name="hibernate.cache.use_second_level_cache" value="false"/>
    
            <property name="hibernate.archive.autodetection" value="true"/>
        </properties>
    </persistence-unit>
    

    1. please note down the information mentioned in the < persistance > tag and version should be 2.1.
    2. please note the name < persistance-unit > tag, name is mentioned as "sample". This name needs to be used exactly same while loading your

    EntityManagerFactor = Persistance.createEntityManagerFactory("sample");. "sample" can be changed as per your naming convention.

    Now create a Entity class. with name as per my example UserDetail, in the package org.pramod.data.object

    UserDetail.java

    package org.pramod.data.object;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    @Entity
    @Table(name = "user_detail")
    public class UserDetail {
        @Id
        @Column(name="user_id")
        private int id;
        @Column(name="user_name")
        private String userName;
    
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getUserName() {
            return userName;
        }
        public void setUserName(String userName) {
            this.userName = userName;
        }   
        @Override
        public String toString() {
           return "UserDetail [id=" + id + ", userName=" + userName + "]";
        }
    }
    

    Now create a class with main method.

    HibernateTest.java

    package org.pramod.hibernate;
    
    import javax.persistence.EntityManager;
    import javax.persistence.EntityManagerFactory;
    import javax.persistence.Persistence;
    import org.pramod.data.object.UserDetail;
    
    public class HibernateTest {
        private static EntityManagerFactory entityManagerFactory;
    
        public static void main(String[] args)  {
            UserDetail user = new UserDetail();
            user.setId(1);
            user.setUserName("Pramod Sharma");
    
            try {
                entityManagerFactory = Persistence.createEntityManagerFactory("sample");
                EntityManager entityManager = entityManagerFactory.createEntityManager();
                entityManager.getTransaction().begin();
                entityManager.persist( user );
                entityManager.getTransaction().commit();
                System.out.println("successfull");
                entityManager.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
    }
    

    Output will be

    UserDetail [id=1, userName=Pramod Sharma]
    Hibernate: drop table if exists user_details
    Hibernate: create table user_details (user_id integer not null, user_name varchar(255), primary key (user_id))
    Hibernate: insert into user_details (user_name, user_id) values (?, ?)
    successfull
    
    0 讨论(0)
  • 2020-11-22 04:38

    In my case it was about mistake in two properties as below. When I changed them ‘No Persistence provider for EntityManager named’ disappered.

    So you could try test connection with your properties to check if everything is correct.

    <property name="javax.persistence.jdbc.url" value="...”/>
    <property name="javax.persistence.jdbc.password" value="...”/>
    

    Strange error, I was totally confused because of it.

    0 讨论(0)
  • 2020-11-22 04:40

    I also had this error but the issue was the namespace uri in the persistence.xml.

    I replaced http://xmlns.jcp.org/xml/ns/persistence to http://java.sun.com/xml/ns/persistence and the version 2.1 to 2.0.

    It's now working.

    0 讨论(0)
提交回复
热议问题