JPA(Entity Bean)开发入门

(一) 系统环境

应用服务器:JBoss AS 4.2.2GA

数据库:MySQL Server 5.0

开发工具:JBossTools 2.0.0 GA

测试工具:TestNG

(二) 数据库准备

1. 创建数测试据库test

Database:test

Username:test

Password:test

2. 创建Person表

DROP TABLE IF EXISTS `test`.`person`;

CREATE TABLE `test`.`person` (

`ID` int(10) unsigned NOT NULL auto_increment,

`Name` varchar(45) NOT NULL,

`Age` int(10) unsigned NOT NULL,

PRIMARY KEY (`ID`)

) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;


(三) 创建EJB项目

1. 在Eclipse中选择菜单“File->New->Project…”,在出现的“New Project”窗口中选择“EJB Project”;在出现“New EJB Project”向导的“EJB Project”窗格中输入如下信息:

Project Name:HelloJPA

Target Runtime:JBoss 4.2 Runtime

选择“Next”继续;

2. 在出现的“Project Facets”窗格中选择如下信息:

EJB Module:3.0

Java:5.0

Java Persistence:1.0

选择“Next”继续;

3. 在出现的“EJB Module”窗格中输入如下信息:

Source Folder:src

4. 在出现的“JPA Facet”窗格中选择“Finish”,EJB项目创建结束。HelloJPA项目的结构如下:



(四) 配置Hibernate 代码生成环境

1. 在Eclipse的菜单中选择“File->New->Other->Hibernate->Hibernate Configuration File(cfg.xml)”;

2. 在出现的“Create Hibernate Configuration File(cfg.xml)”窗格中选择“HelloJPA->src”设置将hibernate.cfg.xml文件放在src文件夹;选择“Next”继续;

3. 在出现的“Hibernate Configuration File(cfg.xml)”窗格中输入如下信息:

Database dialect:MySQL

Driver class:com.mysql.jdbc.Driver

Connection URL:jdbc:mysql://localhost:3306/test

Username:test

Password:test

同时钩选“create a console configuration”,选择“Next”继续;

4. 在出现的“Create Hibernate Console Configuration”窗格中,在“Type”中选择“Core”,选择“Finish”结束Hibernate配置。

(五) 生成EntityBean代码

1. 在Eclipse中切换到“Hibernate”Perspective,在菜单中选择“Run->Hibernate Code Generation…->Open Hibernate Code Generation Dialog…”;

2. 在打开的“Hibernate Code Generation”窗口中,新建一个配置,并钩选如下信息:

Console Configuration:HelloJPA

Output directory:\HelloJPA1\src

钩选“Reverse engineering from JDBC connection”

Package:demo

3. 选择“Exporters”属性页,输入如下信息:

“Uase Java 5 syntax”

“Generate EJB3 annotation”

“Doamin Code”

选择“Finish”将生成EntityBean文件Person.java,内容如下:
package demo; 

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import static javax.persistence.GenerationType.IDENTITY; 
import javax.persistence.Id; 
import javax.persistence.Table; 

@Entity 
@Table(name="person", catalog = "test") 
public class Person implements java.io.Serializable { 


	private Integer id; 
	
	private String name;	
	
	private int age; 

	public Person() { 
	
	} 

	public Person(String name, int age) { 
		this.name = name; 
		this.age = age; 
	} 

	@Id 
	@GeneratedValue(strategy = IDENTITY) 
	@Column(name = "ID", unique = true, nullable = false) 
	public Integer getId() { 
		return this.id; 
	} 

	public void setId(Integer id) { 
		this.id = id; 
	} 

	@Column(name = "Name", nullable = false, length = 45) 
	public String getName() { 
		return this.name; 
	} 
	
	public void setName(String name) { 
		this.name = name; 
	} 

	@Column(name = "Age", nullable = false) 
	public int getAge() { 
		return this.age; 
	} 
	
	public void setAge(int age) { 
		this.age = age; 
	} 


}


(六) 修改Persistence.xml设置

修改后的内容如下:
<?xml version=“1.0″ encoding=“UTF-8″?> 

<persistence version=“1.0″ 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”> 

<persistence-unit name=“HelloJPA” transaction-type=“RESOURCE_LOCAL”> 

<provider>org.hibernate.ejb.HibernatePersistence</provider> 

<class>demo.Person</class> 

<properties> 

<property name=“hibernate.connection.driver_class” value=“com.mysql.jdbc.Driver” /> 

<property name=“hibernate.connection.password” value=“test” /> 

<property name=“hibernate.connection.url” value=“jdbc:mysql://localhost:3306/test” /> 

<property name=“hibernate.connection.username” value=“test” /> 

<property name=“hibernate.dialect” value=“org.hibernate.dialect.MySQLDialect” /> 

</properties> 

</persistence-unit> 

</persistence>


(七) 编写单元测试

1. 在Eclipse中,选择菜单“File->New->Other->Class”,在出现的窗口中输入如下信息:

Package:test

Name:TestPerson

选择“Finish”创建TestPerson.java文件;

2. 编写测试类:TestPerson,具体代码如下:

package demo; 

import javax.persistence.EntityManager; 
import javax.persistence.EntityManagerFactory; 
import javax.persistence.Persistence; 

import org.testng.annotations.*; 


import demo.Person; 

public class TestPerson { 

private EntityManagerFactory factory; 

private EntityManager em; 

	@BeforeTest
	public void setUp() { 
		factory = Persistence.createEntityManagerFactory("HelloJPA"); 
		em = factory.createEntityManager(); 
	} 

	@AfterTest 
	public void tearDown() { 
		em.close(); 
	} 

	@Test (groups = {"unit"}) 
	public void testSave() { 
	
		Person p = new Person(); 
		p.setAge(30); 
		p.setName("www.keqiao8.com"); 
		em.getTransaction().begin(); 
		em.persist(p); 
		assert em.contains(p); 
		em.getTransaction().commit(); 
	} 

}


(八) 执行单元测试

1、 在Eclipse中选择“TestPerson.java”文件,右键单击在出现的上下文菜单中选择“Run as -> TestNG test”

2、 测试通过

3、 打开MySQL Query Browser程序,执行“SELECT * FROM person p”,查询结果表示已经将测试数据保存到Person表中

你可能感兴趣的:(eclipse,Hibernate,mysql,bean,jpa)