jpa是ejb的基礎。現在ejb還沒入門呢,但一定要學下去。從tc中應用ejb的程度來看,這技術在大的應用程序中應用很廣泛。
雖然這次做得很失敗,orm.xml還是沒完全弄明白怎麼寫。但還是將自己所了解的寫上來。HelloWorld example來自<java persistence with hibernate>
首先是persistence.xml文件
xml 代碼
<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="events_event"> <provider>org.hibernate.ejb.HibernatePersistenceprovider> <class>Holidayclass> --> <properties> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.format_sql" value="true" /> <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver" /> <property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:tctest" /> <property name="hibernate.connection.username" value="root" /> <property name="hibernate.connection.password" value="gg" /> <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect" /> properties> persistence-unit> persistence>
接著是orm.xml文件,自己組件的沒做好,但helloworld還是沒問題
xml 代碼
xml version="1.0" encoding="UTF-8"?> <entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd" version="1.0"> <mapped-superclass class="hello.BaseMessage"> <attributes> <id name="id"> <column name="id" column-definition="number(38)" precision="38" scale="0" nullable="false" /> <generated-value strategy="SEQUENCE" generator="SEQ"/> id> attributes> mapped-superclass> <entity class="hello.Message" name="Message"> <table name="MESSAGES">table> <sequence-generator name="SEQ" sequence-name="SQ_messages" allocation-size="1" /> <attribute-override name="id"> <column name="message_id" column-definition="number(18)" nullable="false" precision="18" scale="0"/> attribute-override> <attributes> <basic name="text"> <column name="MESSAGE_TEXT" nullable="false" /> basic> attributes> entity> entity-mappings>
實體定義,用了繼承,不用繼承的早就解決了。注意@MappedSuperclass ,@entity,@Id的用法。需要jpa實現的包。我用的hibernate,這些包主要來自hibernate core 3.2.0g, hibernate entitymanager 3.2.0 and hibernate annotation 3.2. 具體要用其中的哪些我還沒怎麼弄明白,一個個加吧。
java 代碼
package hello; import java.io.Serializable; import javax.persistence.Id; import javax.persistence.MappedSuperclass; @MappedSuperclass public class BaseMessage implements Serializable { private static final long serialVersionUID = 4585624359812256628L; private Long id; public BaseMessage() { super(); } @Id public Long getId() { return id; } public void setId(Long id) { this.id = id; } } package hello; import javax.persistence.Entity; @Entity public class Message extends BaseMessage { private static final long serialVersionUID = -7660981805652763488L; private String text; Message() { } public Message(String text) { this.text = text; } public String getText() { return text; } public void setText(String text) { this.text = text; } }
persistence 過程:
java 代碼
package hello; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; public class HelloWorldEM { public static void main(String[] args) { // Start EntityManagerFactory EntityManagerFactory emf = Persistence.createEntityManagerFactory("helloworld"); // First unit of work EntityManager em = emf.createEntityManager(); EntityTransaction tx = em.getTransaction(); tx.begin(); Message message = new Message("Hello World"); em.persist(message); Long msgId = message.getId(); tx.commit(); em.close(); // Second unit of work 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() + " message(s) found"); for (Object m : messages) { Message loadedMsg = (Message) m; System.out.println(msgId.longValue()); System.out.println(loadedMsg.getText()); } newTx.commit(); newEm.close(); emf.close(); } }