EJB+JPA演示实体类基本操作CRUD的实现

本文详细介绍EJB+JPA实现Entity的CRUD基本操作。

目录

  • 创建POJO:BlogVo
  • 创建Entity:Blog
  • 配置persistence.xml
  • 定义Remote接口
  • 创建SessionBean实现类
  • 配置datasource数据源
  • 部署EJB工程
  • 创建客户端测试类
  • 运行测试类

[一]、创建POJO:BlogVo.java

1 package com.micmiu.ejb.vo;
2  
3 import java.io.Serializable;
4  
5 /**
6  * Blog vo对象
7  *
8  * @author <a href="http://www.micmiu.com">Michael</a>
9  * @time Create on 2013-9-25 下午3:02:17
10  * @version 1.0
11  */
12 public class BlogVo implements Serializable {
13  
14     private static final long serialVersionUID = -6384496304647072095L;
15  
16     private Integer id;
17  
18     private String title;
19  
20     private String url;
21  
22     private String author;
23  
24     public Integer getId() {
25         return id;
26     }
27  
28     public String getTitle() {
29         return title;
30     }
31  
32     public String getUrl() {
33         return url;
34     }
35  
36     public String getAuthor() {
37         return author;
38     }
39  
40     public void setId(Integer id) {
41         this.id = id;
42     }
43  
44     public void setTitle(String title) {
45         this.title = title;
46     }
47  
48     public void setUrl(String url) {
49         this.url = url;
50     }
51  
52     public void setAuthor(String author) {
53         this.author = author;
54     }
55  
56     @Override
57     public String toString() {
58         return "BlogVo [id=" + id + ", title=" + title + ", url=" + url
59                 ", author=" + author + "]";
60     }
61 }

[二]、创建Entity:Blog.java

1 package com.micmiu.ejb.entity;
2  
3 import java.io.Serializable;
4  
5 import javax.persistence.Column;
6 import javax.persistence.Entity;
7 import javax.persistence.GeneratedValue;
8 import javax.persistence.Id;
9 import javax.persistence.NamedQueries;
10 import javax.persistence.NamedQuery;
11 import javax.persistence.Table;
12  
13 /**
14  * 实体类blog
15  *
16  * @author <a href="http://www.micmiu.com">Michael</a>
17  * @time Create on 2013-9-24 下午1:57:47
18  * @version 1.0
19  */
20 @Entity
21 @Table(name = "DEMO_T_BLOG")
22 @NamedQueries({ @NamedQuery(name = "queryAll", query = "select t from Blog t ") })
23 public class Blog implements Serializable {
24  
25     private static final long serialVersionUID = -1371929956020543775L;
26  
27     @Id
28     @GeneratedValue
29     @Column(name = "ID")
30     private Integer id;
31  
32     @Column(name = "TITLE", length = 128)
33     private String title;
34  
35     @Column(name = "URL", length = 1024)
36     private String url;
37  
38     @Column(name = "AUTHOR", length = 20)
39     private String author;
40  
41     public Integer getId() {
42         return id;
43     }
44  
45     public String getTitle() {
46         return title;
47     }
48  
49     public String getUrl() {
50         return url;
51     }
52  
53     public String getAuthor() {
54         return author;
55     }
56  
57     public void setId(Integer id) {
58         this.id = id;
59     }
60  
61     public void setTitle(String title) {
62         this.title = title;
63     }
64  
65     public void setUrl(String url) {
66         this.url = url;
67     }
68  
69     public void setAuthor(String author) {
70         this.author = author;
71     }
72  
73     @Override
74     public String toString() {
75         return "Blog [id=" + id + ", title=" + title + ", url=" + url
76                 ", author=" + author + "]";
77     }
78  
79 }

[三]、配置persistence.xml

修改配置文件 src: META-INF/persistence.xml 

1 <?xml version="1.0" encoding="UTF-8"?>
2 <persistence version="2.0"
3     xmlns="http://java.sun.com/xml/ns/persistence"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4     xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
5              http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
6     <!-- 为持久化单元取名=micmiuJPA,transaction-type=JTA 利用容器的事务管理 -->
7     <persistence-unit name="micmiuJPA" transaction-type="JTA">
8         <provider>org.hibernate.ejb.HibernatePersistence</provider>
9  
10         <jta-data-source>java:/micmiuOracleDS</jta-data-source>
11         <!-- <class>com.micmiu.ejb.entity.Blog</class> -->
12         <!-- 声明是否扫描jar文件中标注了@Enity类.若不扫描,值为true 默认也是true -->
13         <exclude-unlisted-classes>true</exclude-unlisted-classes>
14         <properties>
15             <property name="hibernate.archive.autodetection" value="class,hbm"/>
16             <!--配置Hibernate方言 -->
17             <property name="hibernate.dialect"value="org.hibernate.dialect.Oracle10gDialect" />
18             <!--自动输出schema创建DDL语句 -->
19             <property name="hibernate.hbm2ddl.auto" value="update" />
20             <!-- 输出SQL语句 -->
21             <property name="hibernate.show_sql" value="true" />
22             <!-- 格式化SQL语句 -->
23             <property name="hibernate.format_sql" value="true" />
24         </properties>
25     </persistence-unit>
26 </persistence>

ps:<jta-data-source></jta-data-source>配置的值“java:/micmiuOracleDS”需要和JBOSS_HOME/server/default/conf/tandardjbosscmp-jdbc.xml 中配置的<datasource></datasource>一致,详见 下面 第[六]部分 配置datasource数据源

[四]、定义Remote接口

BlogBeanRemote.java

1 package com.micmiu.ejb;
2  
3 import java.util.List;
4  
5 import javax.ejb.Remote;
6  
7 import com.micmiu.ejb.vo.BlogVo;
8  
9 /**
10  * BlogBean remote
11  *
12  * @author <a href="http://www.micmiu.com">Michael</a>
13  * @time Create on 2013-9-25 下午3:05:42
14  * @version 1.0
15  */
16 @Remote
17 public interface BlogBeanRemote {
18  
19     BlogVo create(BlogVo blog);
20  
21     BlogVo read(Integer id);
22  
23     void update(BlogVo blog);
24  
25     void delete(Integer id);
26  
27     List<BlogVo> queryAll();
28  
29     List<BlogVo> queryByAuthor(String author);
30 }

[五]、创建SessionBean实现类

BlogBean.java

1 package com.micmiu.ejb;
2  
3 import java.util.ArrayList;
4 import java.util.List;

你可能感兴趣的:(jboss,jpa,J2EE,entity,EJB3.0)