hibernate entitymanager的理解

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate entitymanager的理解相关的知识,希望对你有一定的参考价值。

hibernate EntityManager是围绕提供JPA编程接口的Hibernate Core的一个包装,支持JPA实体实例的生命周期,并允许你用标准的Java Persistence查询语言编写查询。

1、基本JPA配置(EntityManagerFactory--EMF配置)

persistence.xml,该文件必须放在被部署的持久化单元的META-INF目录下,由于我这里建的是Java project,所以我把META-INF目录放在bin目录下

<persistence xmlns="http://java.sun.com/xml/ns/persistence"  
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"  
   version="1.0">  
  
   <persistence-unit name="helloworld">  
     
        <provider>org.hibernate.ejb.HibernatePersistence</provider>  
        <!--  
        <class>hello.Message</class> 
         -->  
        <properties>  
            <property name="hibernate.archive.autodetection" value="class,hbm"/>  
              
            <property name="hibernate.connection.username" value="root"/>  
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/myhibernate"/>     
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>     
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>  
            <property name="hibernate.hbm2ddl.auto" value="create"/>  
            <property name="hibernate.c3p0.min_size" value="5"/>  
            <property name="hibernate.c3p0.max_size" value="20"/>  
            <property name="hibernate.c3p0.timeout" value="300"/>  
            <property name="hibernate.c3p0.max_statements" value="50"/>  
            <property name="hibernate.c3p0.idle_test_period" value="3000"/>  
            <property name="hibernate.show_sql" value="true"/>  
            <property name="hibernate.connection.password" value=""/>  
            <property name="myeclipse.connection.profile" value="mysql"/>  
        </properties>  
   </persistence-unit> 
</persistence>

实体类Message.java

    package hello;  
      
    import javax.persistence.CascadeType;  
    import javax.persistence.Column;  
    import javax.persistence.Entity;  
    import javax.persistence.GeneratedValue;  
    import javax.persistence.Id;  
    import javax.persistence.JoinColumn;  
    import javax.persistence.ManyToOne;  
    import javax.persistence.Table;  
      
    @Entity  
    @Table(name = "Message")  
    public class Message {  
      
        @Id @GeneratedValue  
        @Column(name = "ID")  
        private Long id;  
          
        @Column(name = "MESSAGE_TEXT")  
        private String text;  
          
        @ManyToOne(cascade = CascadeType.ALL)  
        @JoinColumn(name = "NEXT_MESSAGE_ID")  
        private Message nextMessage;  
      
        public Long getId() {  
            return id;  
        }  
      
        public void setId(Long id) {  
            this.id = id;  
        }  
      
        public String getText() {  
            return text;  
        }  
      
        public void setText(String text) {  
            this.text = text;  
        }  
      
        public Message getNextMessage() {  
            return nextMessage;  
        }  
      
        public void setNextMessage(Message nextMessage) {  
            this.nextMessage = nextMessage;  
        }  
    }  

测试类HelloWorld.java

    package hello;  
      
    import java.util.Iterator;  
    import java.util.List;  
      
    import javax.persistence.EntityManager;  
    import javax.persistence.EntityManagerFactory;  
    import javax.persistence.EntityTransaction;  
    import javax.persistence.Persistence;  
      
    public class HelloWorld {  
      
        @SuppressWarnings("unchecked")  
        public static void main(String[] args) {  
      
            EntityManagerFactory emf = Persistence  
                    .createEntityManagerFactory("helloworld");  
      
            /*----------1------*/  
            EntityManager em = emf.createEntityManager();  
            EntityTransaction tx = em.getTransaction();  
            tx.begin();  
      
            Message message = new Message();  
            message.setText("hello world");  
            em.persist(message);  
      
            tx.commit();  
            em.close();  
            /*----------2------*/  
            EntityManager newEm = emf.createEntityManager();  
            EntityTransaction newTx = newEm.getTransaction();  
            newTx.begin();  
      
            List messages = newEm.createQuery("select m from Message m order by m.text asc")  
                    .getResultList();  
            System.out.println("messages.size() = " + messages.size());  
      
            for (Iterator iter = messages.iterator(); iter.hasNext();) {  
                Message loadedMsg = (Message) iter.next();  
                System.out.println(loadedMsg.getText());  
            }  
      
            newTx.commit();  
            newEm.close();  
      
            emf.close();  
        }  
      
    }  

运行结果

messages.size() = 1
hello world

说明:

javax.persistence.Persistence   给EntityManagerFactory的创建提供一种静态方法的启动类

javax.persistence.EntityManagerFactory 相当于hibernate的SessionFactory

javax.persistence.EntityManager 相当与hibernate的Session

javax.persistence.Query 相当与hibernate的Query,跟hibernate使用hql一样,同样可以使用对象化的查询语言

javax.persistence.EntityTransaction 相当于hibernate的Transaction


以上是关于hibernate entitymanager的理解的主要内容,如果未能解决你的问题,请参考以下文章

Spring+Hibernate配置中获取EntityManager

使用 EntityManager / Hibernate 进行批量更新

比较JPA的EntityManager接口与Hibernate的Session接口

Hibernate Session和EntityManager之间的区别[重复]

如何使用 Hibernate (EntityManager) 或 JPA 调用 Oracle 函数或过程

Hibernate5.0 EntityManager转SqlQuery并返回Map结果集