EJB实例教程笔记(四)
EJB实例教程笔记(四)
电子书EJB3实例教程byLiHuoming.pdf笔记
第三章 实体Bean(Entity Bean)
在EJB的早期版本中,持久化是EJB平台的一部分。从EJB3.0开始,持久化已经自成规范,被称为Java Persistence API。
POJO通过Java Persistence元数据映射到数据库后,我们称entity bean。
创建一个Entity Bean对象相当于新建一条记录
删除一个Entity Bean会同时从数据库中删除对应记录
修改一个Entity Bean,容器会自动将Entity Bean的状态同步到数据库。
Java Persistence API还定义了一种查询语言(JPQL)。
3.1 JBOSS数据源的配置
取名格式为xxx-ds.xml
3.1.1 MySql数据源的配置
sample-ds.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>demoDS</jndi-name>
<connection-url>jdbc:mysql://localhost:3307/demo?useUnicode=true&characterEncoding=GBK</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password></password>
<min-pool-size>1</min-pool-size>
<max-pool-size>4</max-pool-size>
<!-- 抛出异常前最大的等待链接时间 -->
<blocking-timeout-millis>30000</blocking-timeout-millis>
<!-- 关闭连接前链接空闲的最大时间 -->
<idel-timeout-minutes>5</idel-timeout-minutes>
<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>
3.1.2 Ms Sql Server2000数据源的配置
3.1.3 Oracle9i数据源的配置
3.2 单表映射的实体Bean
JSP Engine ---------> EntityBeanTest.jsp
SessionBean EJB Container ---------> PersonDAOBean.class
Java Persistence API ----------> Person.class(EJB annotated POJOs)
create database table:
CREATE TABLE `test`.`person` (
`id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(32) NOT NULL,
`sex` TINYINT(1) UNSIGNED NOT NULL,
`age` SMALLINT(6) UNSIGNED NOT NULL,
`birthday` DATETIME,
PRIMARY KEY (`id`)
)
ENGINE = InnoDB;
POJO annotation class Person.java:
package com.sillycat.ejb3.model;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Table(name = "person")
public class Person implements Serializable
{
private static final long serialVersionUID = 7290155616161659440L;
private Integer id;
private String name;
private boolean sex;
private Short age;
private Date birthday;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
@Column(nullable=false,length=32)
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
@Column(nullable=false)
public boolean getSex()
{
return sex;
}
public void setSex(boolean sex)
{
this.sex = sex;
}
@Column(nullable=false)
public Short getAge()
{
return age;
}
@Temporal(value=TemporalType.DATE)
public void setAge(Short age)
{
this.age = age;
}
public Date getBirthday()
{
return birthday;
}
public void setBirthday(Date birthday)
{
this.birthday = birthday;
}
}
实体Bean不直接与客户端打交道。而是被session bean或message-driven bean使用。
接口类PersonDAO.java:
package com.sillycat.ejb3.dao;
import java.util.List;
import com.sillycat.ejb3.model.Person;
public interface PersonDAO
{
public void insert(Person person);
public void updateName(String name,Integer id);
public void update(Person person);
public void delete(Integer id);
public Person get(Integer id);
public List<Person> getAll();
}
实现类PersonDAOBean.java:
package com.sillycat.ejb3.dao;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import com.sillycat.ejb3.model.Person;
public class PersonDAOBean implements PersonDAO
{
@PersistenceContext(unitName = "demoTest")
protected EntityManager em;
public PersonDAOBean(EntityManager em)
{
this.em = em;
}
public void delete(Integer id)
{
Person person = em.find(Person.class, id);
if (person != null)
{
em.remove(person);
}
}
public Person get(Integer id)
{
return em.find(Person.class, id);
}
@SuppressWarnings("unchecked")
public List<Person> getAll()
{
Query query = em.createQuery("select o from Person o order by o.id asc");
return (List<Person>) query.getResultList();
}
public void insert(Person person)
{
em.persist(person);
}
public void update(Person person)
{
em.merge(person);
}
public void updateName(String name, Integer id)
{
Person person = em.find(Person.class, id);
if (person != null)
{
person.setName(name);
}
}
}
JPA的配置文件persistence.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="demoTest" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="kaishi" />
<property name="hibernate.connection.autocommit" value="true"/>
</properties>
</persistence-unit>
</persistence>
如果发布给jboss使用,应该是persistence.xml:
<?xml version="1.0" ?>
<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="demoTest" transaction-type="JTA">
<jta-data-source>java:/DefaultDemoTestDS</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.jdbc.fetch_size" value="18"/>
<property name="hibernate.jdbc.batch_size" value="10" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
里面的DefaultDemoTestDS就是jboss下面配置的数据库链接的文件了。