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(); } } |
【编辑推荐】
- 详解XML与J2EE组合技术的精髓
- J2EE的13种核心技术简介
- 深入掌握Java技术 EJB调用原理分析