- 浏览: 215979 次
- 性别:
- 来自: 广州
-
文章分类
最新评论
-
liuyanpersonal:
你好啊
单元测试SpringMVC带文件上传的controller -
kkgoing:
teamPhotoController 这个Controlle ...
单元测试SpringMVC带文件上传的controller -
尘枉_yjava:
gwt-ext Out of memory解决办法?? 亲,办 ...
gwt-ext Out of memory解决办法 -
张传龙:
多谢大侠,我找了好久才找到
单元测试SpringMVC带文件上传的controller -
argel_lj:
关键是提示这些信息,而且后面还提示build successf ...
maven经常报Unable to find resource in repository
新建一个EJB3项目,在WETA-INF目录下新建persistence.xml文件,其中"hibernate.hbm2ddl.auto" value="create-drop",则不需要人工建立数据库表,会自动帮你建立。hibernate.dialect如果没写,也会提示出错。
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence>
<persistence-unit name="Ejb3">
<jta-data-source>java:/ejb3Example</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto"
value="create-drop"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
</properties>
</persistence-unit>
</persistence>
建立entity bean:Book.java,这是一个普通POJO,里面使用JPA注释它是一个entity bean,在领域模型里,实体类继承Serializable接口实现序列化,有利于使用缓存。如果有提示@Table(name="book")
出错,找不到数据库表,不用管它,最后系统会自动建立数据库表,当然,自己手动建立一个也可。
Book.java
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
@Entity
@Table(name="book")
@TableGenerator( name="book_id", table="primary_keys", pkColumnName="key", pkColumnValue="book",valueColumnName="value")
public class Book implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String title;
private String author;
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "book_id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Book() {
super();
}
public Book(Integer id, String title, String author) {
super();
this.id = id;
this.title = title;
this.author = author;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Book: " + getId() + " Title " + getTitle() + " Author "
+ getAuthor();
}
}
建立一个session bean:BookTestBean.java,并添加本地和远程接口BookTestBeanLocal.java、BookTestBeanRemote.java
BookTestBean.java
import java.util.Iterator;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class BookTestBean implements BookTestBeanLocal, BookTestBeanRemote {
@PersistenceContext
EntityManager em;
public static final String RemoteJNDIName = BookTestBean.class.getSimpleName() +
"/remote";
public static final String LocalJNDIName = BookTestBean.class.getSimpleName() +
"/local";
public void test() {
Book book = new Book(null, "My first bean book", "Sebastian");
em.persist(book);
Book book2 = new Book(null, "another book", "Paul");
em.persist(book2);
Book book3 = new Book(null, "EJB 3 developer guide, comes soon",
"Sebastian");
em.persist(book3);
System.out.println("list some books");
List someBooks = em.createQuery("from Book b where b.author=:name")
.setParameter("name", "Sebastian").getResultList();
for (Iterator iter = someBooks.iterator(); iter.hasNext();)
{
Book element = (Book) iter.next();
System.out.println(element);
}
System.out.println("List all books");
List allBooks = em.createQuery("from Book").getResultList();
for (Iterator iter = allBooks.iterator(); iter.hasNext();)
{
Book element = (Book) iter.next();
System.out.println(element);
}
System.out.println("delete a book");
em.remove(book2);
System.out.println("List all books");
allBooks = em.createQuery("from Book").getResultList();
for (Iterator iter = allBooks.iterator(); iter.hasNext();)
{
Book element = (Book) iter.next();
System.out.println(element);
}
}
}
BookTestBeanLocal.java
import javax.ejb.Local;
@Local
public interface BookTestBeanLocal {
public void test();
}
BookTestBeanRemote.java
import javax.ejb.Remote;
@Remote
public interface BookTestBeanRemote {
public void test();
}
在%JBOSS_HOME%\server\default\deploy下新建文件EJB-DS.XML,相应的数据库写法,可在%JBOSS_HOME%\docs\examples\jca中找到。
EJB-DS.XML
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>ejb3Example</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/mysql</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>xx</user-name>
<password>xx</password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
这是所用到的包。
在JBOSS中部署成jar,查看是否部署成功。
编写测试客户端TestClient.java,并在META-INF下添加jndi.properies。
TestClient.java
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class TestClient.java{
/**
* @param args
*/
public static void main(String[] args) {
Context context;
try
{
context = new InitialContext();
BookTestBeanRemote beanRemote = (BookTestBeanRemote) context.lookup(BookTestBean.RemoteJNDIName);
beanRemote.test();
} catch (NamingException e)
{
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
jndi.properies
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099
此时客户端测试只需要一个J2EE包和jbossall-client.jar便可。如果客户端与服务器端的包有冲突或版本不同,就会有stream classdesc serialVersionUID = 4582256576523491346, local class serialVersionUID = 3844706474734439975这样的序列化出错。
最后运行客户端测试。
发表评论
-
Apache+tomcat均衡负载配置新小结
2012-03-23 17:27 3404Apache+tomcat均衡负载配 ... -
'IHTMLControlElement' : redefinition 报错
2012-03-13 11:04 1949在设置好新的platform sdk之后,编译原有系统报: D ... -
iis设置isapi应用
2012-02-29 21:16 1077不需要设置isapi筛选器,直接拷贝dll到网站下,输入dll ... -
wince下httpd总是跳去远程管理页面的解决
2012-02-29 21:12 953将pb生成的release目录下的servers.reg里的: ... -
spring下jotm jta实现小结
2012-02-13 15:27 2009需求是从sql2005读取数据,然后写到另外一个oracle库 ... -
sql2005循环执行插入
2012-01-05 09:56 1007declare @rowNo int set @rowNo=1 ... -
SQL Server 2005 导出包含(insert into)数据的SQL脚本 (转)
2011-12-01 17:40 3586CREATE PROCEDURE dbo.UspOutputD ... -
linux 下 rman can't open target 的报错
2011-11-21 11:54 1049把home/oracle下的.bash_profile修改为, ... -
flex获取图片旋转后的坐标
2011-09-09 17:31 1770public function getRotateXY( ob ... -
maven经常报Unable to find resource in repository
2011-08-10 10:41 2069maven经常报Unable to find resource ... -
FLEX 图片旋转后的拖动
2011-05-21 13:03 1551图片没旋转之前拖动很正常。左旋转之后拖动会不正常。那是因为旋转 ... -
IE8 上传图片预览的JS(转)
2011-05-06 16:37 1386function photoPreView(image) { ... -
Spring ROO 快速整合BlazeDS
2011-04-29 16:45 23561、首先安装最新版本SpringSourceToolSu ... -
FLEX4 照片合成,支持拖动
2011-04-26 08:48 1572flex 4.5 下测试通过,将2张照片合成后生成合成图 & ... -
jni调用报错UnsatisfiedLinkError
2011-04-20 15:38 945报类似 UnsatisfiedLinkError: com.x ... -
GraphicsMagick处理图片
2011-04-19 10:02 1071//改变原图尺寸gm convert src.jpg -r ... -
flex 4.5 切换states后DataGrid的ColumnHeade变成空白
2011-03-25 14:42 1111flex 4.5 切换states后DataGrid的Colu ... -
090505 james邮件自动跑去spam box下面的问题
2009-05-05 17:16 1492解决用james收邮件时邮件自己跑去了spam box下面的问 ... -
071719 rspec在netbeans IED 6 beta上运行时的一些问题
2007-11-19 15:45 617不知是我弄错了什么地方,还是本来就是这样的,在netbeans ... -
用keytool生成安全证书
2008-03-04 18:00 437用keytool生成证书: keytool - ...
相关推荐
**EJB3.0开发入门 Step by Step** Enterprise JavaBeans(EJB)是Java平台企业版(J2EE,现在称为Java EE)的核心组件之一,它提供了一种规范化的、可扩展的方式来创建分布式的企业级应用。EJB3.0是EJB规范的一个...
### Struts Step by Step 教程 #### 1. 前言 本文档旨在指导开发者逐步构建一个基于Struts框架的应用程序。本教程适用于初学者以及具有一定经验的开发人员,帮助他们熟悉Struts框架的基本组件及其使用方式。文档...
3. **创建应用**:利用Oracle JHeadstart 10g根据Oracle HR样本数据库自动生成包含浏览、搜索、插入、更新和删除功能的应用程序。 4. **定制与优化**:对生成的应用进行必要的定制和优化,如添加自定义的UI组件、...
Step by step and easy to follow, this book describes many of the Java EE 6 specifications and reference implementations, and shows them in action using practical examples. This book uses the new ...
3.6.3 Exercise Step-by-Step Solution 3.7 Summary 4 Working with the Enterprise Services Repository and Registry 4.1 Basic ES Repository Technical Concepts 4.1.1 Functional Blocks 4.1.2 First ...
TestNG 的创造者是 Cedric Beust,他在 Java 编程领域非常出名,是 EJB 3 专家组的成员,也是其他一些流行的开源项目(例如 EJBGen 和 Doclipse)的创造者。 示例测试代码: package example1; import org.testng....
10. **学习资料**:提供的文档,如`java的JNDI 技术介绍及应用.docx`和`jndi step by step - aurawing - 博客园.pdf`,可能包含JNDI的详细教程和实践指南。`Untitled-1.html`可能是另一个相关文档,虽然其确切内容...
- **Installation**: Step-by-step instructions for installing XDoclet and configuring it for use. - **Basic Usage**: Simple examples demonstrating how to use XDoclet tags and generate basic code ...
This guide takes a step-by-step approach to developing, testing, debugging, and troubleshooting JEE applications, complete with examples and tips. Table of Contents Chapter 1. Introducing JEE and ...
This book guides you step by step through topics using complete and real-world code examples. Instead of abstract descriptions on complex concepts, you will find live examples in this book. When you ...
3. **Spring-MVC-step-by-step.pdf**:这是一个Spring MVC的逐步教程,适合想要深入理解Spring MVC架构和工作流程的读者。它可能会涵盖从搭建环境、创建控制器、处理请求到视图渲染的全过程,帮助你掌握Web应用开发...
`Spring-MVC-step-by-step.pdf`是Spring MVC的分步教程,Spring MVC是Spring框架的一部分,专门用于构建Web应用程序。该教程将引导你逐步创建一个Spring MVC项目,从配置环境到编写控制器、视图和模型,直至实现数据...
Converting existing Java artifacts into OSGi bundles is a crucial step for leveraging the benefits of modularity and dynamicity provided by OSGi. Key considerations include: - **Manifest Headers**: ...