首页 > 代码库 > hibernate entitymanager的理解

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="http://www.mamicode.com/class,hbm"/>  
              
            <property name="hibernate.connection.username" value="http://www.mamicode.com/root"/>  
            <property name="hibernate.connection.url" value="http://www.mamicode.com/jdbc:MySQL://localhost:3306/myhibernate"/>     
            <property name="hibernate.dialect" value="http://www.mamicode.com/org.hibernate.dialect.MySQLDialect"/>     
            <property name="hibernate.connection.driver_class" value="http://www.mamicode.com/com.mysql.jdbc.Driver"/>  
            <property name="hibernate.hbm2ddl.auto" value="http://www.mamicode.com/create"/>  
            <property name="hibernate.c3p0.min_size" value="http://www.mamicode.com/5"/>  
            <property name="hibernate.c3p0.max_size" value="http://www.mamicode.com/20"/>  
            <property name="hibernate.c3p0.timeout" value="http://www.mamicode.com/300"/>  
            <property name="hibernate.c3p0.max_statements" value="http://www.mamicode.com/50"/>  
            <property name="hibernate.c3p0.idle_test_period" value="http://www.mamicode.com/3000"/>  
            <property name="hibernate.show_sql" value="http://www.mamicode.com/true"/>  
            <property name="hibernate.connection.password" value=""/>  
            <property name="myeclipse.connection.profile" value="http://www.mamicode.com/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的理解