No Persistence provider for EntityManager named

前端 未结 30 2601
甜味超标
甜味超标 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: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

    
    
    
    
    
    
        org.pramod.data.object.UserDetail
        true
        
            
            
            
            
            
            
            
            
    
            
    
            
        
    
    

    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
    

提交回复
热议问题