1. 程式人生 > >hibernate entitymanager的理解

hibernate entitymanager的理解

idl .profile www port 軟件測試 get val nbsp cnblogs

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的理解