2007-06-22

jpa技术总结

jpa是ejb的基础。现在ejb还没入门呢,但一定要学下去。从tc中应用ejb的程度来看,这技术在大的应用程序中应用很广泛。
虽然这次做得很失败,orm.xml还是没完全弄明白怎么写。但还是将自己所了解的写上来。HelloWorld example来自<java persistence with hibernate>
首先是persistence.xml文件
xml 代码
 
  1. <persistence xmlns="http://java.sun.com/xml/ns/persistence"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xsi:schemaLocation="http://java.sun.com/xml/ns/persistence  
  4. http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"  
  5.     version="1.0">  
  6.     <persistence-unit name="events_event">  
  7.         <provider>org.hibernate.ejb.HibernatePersistenceprovider>  
  8.         
  9.             <class>Holidayclass>  
  10.         -->  
  11.         <properties>  
  12.             <property name="hibernate.show_sql" value="true" />  
  13.             <property name="hibernate.format_sql" value="true" />  
  14.             <property name="hibernate.connection.driver_class"  
  15.                 value="oracle.jdbc.driver.OracleDriver" />  
  16.             <property name="hibernate.connection.url"  
  17.                 value="jdbc:oracle:thin:@localhost:1521:tctest" />  
  18.             <property name="hibernate.connection.username" value="root" />  
  19.             <property name="hibernate.connection.password" value="gg" />  
  20.             <property name="hibernate.dialect"  
  21.                 value="org.hibernate.dialect.OracleDialect" />  
  22.         properties>  
  23.     persistence-unit>  
  24.       
  25. persistence>  

接着是orm.xml文件,自己组件的没做好,但helloworld还是没问题

xml 代码
 
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd" version="1.0">  
  5.   
  6.     <mapped-superclass class="hello.BaseMessage">  
  7.         <attributes>  
  8.             <id name="id">  
  9.                 <column name="id" column-definition="number(38)" precision="38" scale="0" nullable="false" />  
  10.                 <generated-value strategy="SEQUENCE" generator="SEQ"/>  
  11.             id>  
  12.         attributes>  
  13.     mapped-superclass>  
  14.           
  15.     <entity class="hello.Message" name="Message">  
  16.         <table name="MESSAGES">table>  
  17.           
  18.         <sequence-generator name="SEQ" sequence-name="SQ_messages" allocation-size="1" />  
  19.   
  20.         <attribute-override name="id">  
  21.             <column name="message_id" column-definition="number(18)" nullable="false" precision="18" scale="0"/>  
  22.         attribute-override>  
  23.   
  24.         <attributes>  
  25.             <basic name="text">  
  26.                 <column name="MESSAGE_TEXT" nullable="false" />  
  27.             basic>  
  28.         attributes>  
  29.     entity>  
  30. entity-mappings>  

实体定义,用了继承,不用继承的早就解决了。注意@MappedSuperclass ,@entity,@Id的用法。需要jpa实现的包。我用的hibernate,这些包主要来自hibernate core 3.2.0g, hibernate entitymanager 3.2.0 and hibernate annotation 3.2. 具体要用其中的哪些我还没怎么弄明白,一个个加吧。

java 代码
 
  1. package hello;  
  2. import java.io.Serializable;  
  3.   
  4. import javax.persistence.Id;  
  5. import javax.persistence.MappedSuperclass;  
  6. @MappedSuperclass  
  7. public class BaseMessage implements Serializable {  
  8.     private static final long serialVersionUID = 4585624359812256628L;  
  9.     private Long id;  
  10.     public BaseMessage() {  
  11.         super();  
  12.     }  
  13.     @Id  
  14.     public Long getId() {  
  15.         return id;  
  16.     }  
  17.     public void setId(Long id) {  
  18.         this.id = id;  
  19.     }  
  20. }  
  21.   
  22. package hello;  
  23. import javax.persistence.Entity;  
  24. @Entity  
  25. public class Message extends BaseMessage {  
  26.     private static final long serialVersionUID = -7660981805652763488L;  
  27.     private String text;  
  28.     Message() {  
  29.     }  
  30.     public Message(String text) {  
  31.         this.text = text;  
  32.     }  
  33.     public String getText() {  
  34.         return text;  
  35.     }  
  36.     public void setText(String text) {  
  37.         this.text = text;  
  38.     }  
  39. }  

persistence 过程:
java 代码
 
  1. package hello;  
  2.   
  3. import java.util.List;  
  4.   
  5. import javax.persistence.EntityManager;  
  6. import javax.persistence.EntityManagerFactory;  
  7. import javax.persistence.EntityTransaction;  
  8. import javax.persistence.Persistence;  
  9.   
  10. public class HelloWorldEM {  
  11.     public static void main(String[] args) {  
  12.         // Start EntityManagerFactory  
  13.         EntityManagerFactory emf = Persistence.createEntityManagerFactory("helloworld");  
  14.         // First unit of work  
  15.         EntityManager em = emf.createEntityManager();  
  16.         EntityTransaction tx = em.getTransaction();  
  17.         tx.begin();  
  18.         Message message = new Message("Hello World");  
  19.         em.persist(message);  
  20.         Long msgId = message.getId();  
  21.         tx.commit();  
  22.         em.close();  
  23.   
  24.         // Second unit of work  
  25.         EntityManager newEm = emf.createEntityManager();  
  26.         EntityTransaction newTx = newEm.getTransaction();  
  27.         newTx.begin();  
  28.         List messages = newEm.createQuery("select m from Message m order by m.text asc").getResultList();  
  29.         System.out.println(messages.size() + " message(s) found");  
  30.         for (Object m : messages) {  
  31.             Message loadedMsg = (Message) m;  
  32.             System.out.println(msgId.longValue());  
  33.             System.out.println(loadedMsg.getText());  
  34.         }  
  35.         newTx.commit();  
  36.         newEm.close();  
  37.         emf.close();  
  38.     }  
  39. }  
评论
发表评论

您还没有登录,请登录后发表评论