EJB实体bean开发

开发步骤

1.编写Person.java实体类

2.开发PersonService.java接口类

3.开发PersonServiceBean.java实现类

4.发布EntityBean.jar文件到jboss-4.2.3.GA-jdk6\jboss-4.2.3.GA\server\default\deploy\下

发布的文件只有Person.java、PersonService.java、PersonServiceBean.java、METH-INF/persistence.xml、jndi.properties等文件

5.编写mysql-ds.xml数据源文件,部署到jboss-4.2.3.GA-jdk6\jboss-4.2.3.GA\server\default\deploy\下

6.复制mysql-connector-java-5.1.8-bin.jar文件到jboss-4.2.3.GA-jdk6\jboss-4.2.3.GA\server\default\lib\下

7.编写juit的PersonServiceTest.java测试类

 

Person.java

package cn.cmcc.bean;

 

import java.io.Serializable;

 

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

import javax.persistence.Table;

 

@Entity

@Table(name="person")

public class Person implements Serializable{

 

private static final long serialVersionUID = 7907974948739540719L;

 

@Id @Column(name="id") @GeneratedValue(strategy=GenerationType.AUTO)

private Integer id;

@Column(name="name",length=20,nullable=false)  

private String name;

 

public Person(){}

 

public Person(String name){

this.name=name;

 

}

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + ((id == null) ? 0 : id.hashCode());

return result;

}

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

Person other = (Person) obj;

if (id == null) {

if (other.id != null)

return false;

} else if (!id.equals(other.id))

return false;

return true;

}

 

 

}

 

 

PersonService.java

package cn.cmcc.service;

 

import java.util.List;

 

import cn.cmcc.bean.Person;

 

public interface PersonService {

 

public void sava(Person person);

public void delete(Integer personId);

public void update(Person person);

public Person getPerson(Integer id);

public List persons();

}

 

 

PersonServiceBean.java

package cn.cmcc.service.impl;

 

import java.util.List;

 

import javax.ejb.Remote;

import javax.ejb.Stateless;

import javax.persistence.EntityManager;

import javax.persistence.PersistenceContext;

 

import cn.cmcc.bean.Person;

import cn.cmcc.service.PersonService;

 

@Stateless

@Remote

public class PersonServiceBean implements PersonService {

//会自动去classes/META-INF下读取persistence.xml,当persistence.xml中有多个unit时,需要指定PersistenceContext 中unitName属性的值

    @PersistenceContext EntityManager em;

    

@Override

public void delete(Integer personId) {

em.remove(em.getReference(Person.class, personId));

 

}

 

@Override

public Person getPerson(Integer id) {

return em.find(Person.class, id);

}

 

@SuppressWarnings("unchecked")

@Override

public List persons() {

 

return em.createQuery("select o from Person o ").getResultList();

}

 

@Override

public void sava(Person person) {

em.persist(person);

}

 

@Override

public void update(Person person) {

em.merge(person);

}

 

}

 

 

persistence.xml

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">

 

java:cmcc

 

 

 

 

 

jndi.properties

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory

java.naming.provider.url=localhost:1099

 

 

PersonServiceTest.java

package junti.test;

 

 

import javax.naming.InitialContext;

 

import org.junit.BeforeClass;

import org.junit.Test;

 

import cn.cmcc.bean.Person;

import cn.cmcc.service.PersonService;

 

public class PersonServiceTest {

 

private static PersonService personService;

@BeforeClass

public static void setUpBeforeClass() throws Exception {

 

try {

InitialContext ctx = new InitialContext();

personService=(PersonService)ctx.lookup("PersonServiceBean/remote");

} catch (Exception e) {

 

e.printStackTrace();

}

 

}

 

@Test

public void testSava() {

personService.sava(new Person("春"));

}

 

@Test

public void testDelete() {

personService.delete(1);

}

 

@Test

public void testUpdate() {

Person person=personService.getPerson(1);

person.setName("chun");

personService.update(person);

}

 

@Test

public void testGetPerson() {

personService.getPerson(1);

}

 

@Test

public void testPersons() {

personService.persons();

}

 

}

 

 

你可能感兴趣的:(java)