jpa技术总结

开发 后端
jpa是ejb的基础。现在ejb还没入门呢,但一定要学下去。从tc中应用ejb的程度来看,这技术在大的应用程序中应用很广泛。

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();
}
}

【编辑推荐】

  1. 详解XML与J2EE组合技术的精髓
  2. J2EE的13种核心技术简介
  3. 深入掌握Java技术 EJB调用原理分析
责任编辑:张攀 来源: 新浪
相关推荐

2013-11-12 09:39:41

IGMP组播

2012-05-14 17:21:19

ibmdw

2012-03-06 11:25:40

ibmdw

2009-07-29 17:36:55

ibmdwJava

2021-10-06 23:31:45

HibernateJPASpring Data

2011-08-24 13:24:52

2010-06-30 09:51:55

UML建模技术

2018-01-09 15:57:18

热修复开发编译

2016-12-19 10:00:00

React性能优化

2013-09-04 14:25:30

webkitwebApp开发

2015-09-09 10:20:00

php缓存技术

2010-03-25 17:52:11

2009-11-03 17:27:07

2015-09-14 15:34:12

PHP缓存技术

2009-10-29 13:37:59

宽带接入技术

2019-08-12 15:17:23

USB模糊测试

2022-05-27 11:46:48

技术能力思考

2009-06-26 16:01:39

EJB组织开发EJB容器EJB

2009-10-10 10:34:10

微软虚拟化技术

2018-03-23 18:07:20

存储
点赞
收藏

51CTO技术栈公众号