自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

jpa技術(shù)總結(jié)

開發(fā) 后端
jpa是ejb的基礎(chǔ)?,F(xiàn)在ejb還沒入門呢,但一定要學(xué)下去。從tc中應(yīng)用ejb的程度來看,這技術(shù)在大的應(yīng)用程序中應(yīng)用很廣泛。

jpa是ejb的基礎(chǔ)?,F(xiàn)在ejb還沒入門呢,但一定要學(xué)下去。從tc中應(yīng)用ejb的程度來看,這技術(shù)在大的應(yīng)用程序中應(yīng)用很廣泛。
雖然這次做得很失敗,orm.xml還是沒完全弄明白怎么寫。但還是將自己所了解的寫上來。HelloWorld example來自<java persistence with hibernate>
首先是persistence.xml文件

xml 代碼

<persistence xmlns="xmlns:xsi="xsi:schemaLocation="http://java.sun.com/xml/ns/persistence   
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="xmlns:xsi="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>


實(shí)體定義,用了繼承,不用繼承的早就解決了。注意@MappedSuperclass ,@entity,@Id的用法。需要jpa實(shí)現(xiàn)的包。我用的hibernate,這些包主要來自hibernate core 3.2.0g, hibernate entitymanager 3.2.0 and hibernate annotation 3.2. 具體要用其中的哪些我還沒怎么弄明白,一個(gè)個(gè)加吧。

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組合技術(shù)的精髓
  2. J2EE的13種核心技術(shù)簡介
  3. 深入掌握J(rèn)ava技術(shù) EJB調(diào)用原理分析
責(zé)任編輯:張攀 來源: 新浪
相關(guān)推薦

2013-11-12 09:39:41

IGMP組播

2012-05-14 17:21:19

ibmdw

2012-03-06 11:25:40

ibmdw

2010-06-30 09:51:55

UML建模技術(shù)

2018-01-09 15:57:18

熱修復(fù)開發(fā)編譯

2011-08-24 13:24:52

2009-07-29 17:36:55

ibmdwJava

2021-10-06 23:31:45

HibernateJPASpring Data

2015-09-09 10:20:00

php緩存技術(shù)

2010-03-25 17:52:11

2013-09-04 14:25:30

webkitwebApp開發(fā)

2016-12-19 10:00:00

React性能優(yōu)化

2009-10-28 09:21:19

VB.NET技術(shù)

2015-08-25 08:57:57

android6.0技術(shù)總結(jié)

2009-10-10 10:34:10

微軟虛擬化技術(shù)

2018-03-23 18:07:20

存儲(chǔ)

2009-06-26 16:01:39

EJB組織開發(fā)EJB容器EJB

2009-11-03 17:27:07

2009-10-29 13:37:59

寬帶接入技術(shù)

2015-09-14 15:34:12

PHP緩存技術(shù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)