jpa是ejb的基础。现在ejb还没入门呢,但一定要学下去。从tc中应用ejb的程度来看,这技术在大的应用程序中应用很广泛。
虽然这次做得很失败,orm.xml还是没完全弄明白怎么写。但还是将自己所了解的写上来。HelloWorld example来自
首先是persistence.xml文件
xml 代码
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"> org.hibernate.ejb.HibernatePersistenceprovider> Holidayclass> --> value="oracle.jdbc.driver.OracleDriver" /> value="jdbc:oracle:thin:@localhost:1521:tctest" /> value="org.hibernate.dialect.OracleDialect" /> properties> persistence-unit> persistence> |
接着是orm.xml文件,自己组件的没做好,但helloworld还是没问题
xml 代码
xml version="1.0" encoding="UTF-8"?> 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"> id> attributes> mapped-superclass>
table> attribute-override> 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调用原理分析
相关内容
|