在前面的所有的实体Bean的例子中,我们所用的主键都是Id,而且都是由容器自动生成的。
如:
@Id(generate = GeneratorType.AUTO)
public int getId()
{
return id;
}
事实上,主键可以是任意的java基本类型、或者是基本类型的包装类,如Integer、String类型、或者是带字段或属性的主键类。注意,如果这样使用主键,你需要将主键的注释中的生成规则改为NONE。就是由程序生成主键值。
主键类必须实现hashCode和equals方法。这个例子前面举例过,就是一个存储学生信息的实体bean,在那个例子中,使用Name类作为属性,并且以Id作为主键,在这个例子中,我们将使用name作为主键,注意name的类型一个java类。和前面的例子一样,我们还是使用Client测试。
Name.java:主键。
Student.java:实体Bean类。
StudentDAO.java:会话Bean的业务接口
StudentDAOBean.java:会话Bean的实现类
Client.java:测试EJB的客户端类。
jndi.properties:jndi属性文件,提供访问jdni的基本配置属性。
Build.xml:ant 配置文件,用以编译、发布、测试、清除EJB。
下面针对每个文件的内容做一个介绍。
Name.java
package com.kuaff.ejb3.composite;
import java.io.Serializable;
import javax.ejb.AccessType;
import javax.ejb.DependentObject;
@DependentObject(access = AccessType.PROPERTY)
public class Name implements java.io.Serializable
{
private String first;
private String last;
public Name()
{
}
public Name(String first, String last)
{
this.first = first;
this.last = last;
}
public String getFirst()
{
return first;
}
public void setFirst(String first)
{
this.first = first;
}
public String getLast()
{
return last;
}
public void setLast(String last)
{
this.last = last;
}
public int hashCode()
{
return (first+last).hashCode();
}
public boolean equals(Object object)
{
if (this == object) return true;
if (object == null) return false;
if (! (object instanceof Name)) return false;
Name name = (Name)object;
if ((name.first.equals(first)) && (name.last.equals(last)))
return true;
else
return false;
}
}
Student.java
package com.kuaff.ejb3.composite;
import javax.ejb.Dependent;
import javax.ejb.DependentAttribute;
import javax.ejb.Column;
import javax.ejb.Entity;
import javax.ejb.GeneratorType;
import javax.ejb.Id;
import javax.ejb.Table;
@Entity @Table(name = "STUDENT") public class Student implements java.io.Serializable
{
private Name name;
private String grade;
private String email;
public void setName(Name name)
{
this.name = name;
}
@Id(generate = GeneratorType.NONE)
@Dependent(
{ @DependentAttribute(name = "first", column ={ @Column(name = "FIRST") }),
@DependentAttribute(name = "last", column ={ @Column(name = "LAST") }) })
public Name getName()
{
return name;
}
public void setGrade(String grade)
{
this.grade = grade;
}
@Column(name = "GRADE")
public String getGrade()
{
return grade;
}
public void setEmail(String email)
{
this.email = email;
}
@Column(name = "EMAIL")
public String getEmail()
{
return email;
}
}
StudentDAO.java
package com.kuaff.ejb3.composite;
import javax.ejb.Remote;
import java.util.List;
@Remote
public interface StudentDAO
{
void create(String first, String last, String grade, String email);
Student find(Name name);
List findByFirstName(String name);
List findByLastName(String name);
List findByEmail(String email);
void merge(Student s);
}
StudentDAOBean.java
package com.kuaff.ejb3.composite;
import java.util.List;
import javax.ejb.EntityManager;
import javax.ejb.Inject;
import javax.ejb.Stateless;
@Stateless
public class StudentDAOBean implements StudentDAO
{
@Inject
private EntityManager manager;
public void create(String first, String last, String grade, String email)
{
Student student = new Student();
student.setName(new Name(first,last));
student.setGrade(grade);
student.setEmail(email);
manager.create(student);
}
public Student find(Name name)
{
return manager.find(Student.class, name);
}
public List findByFirstName(String name)
{
return manager.createQuery("from Student s where s.name.last = :name").setParameter("name", name).listResults();
}
public List findByLastName(String name)
{
return manager.createQuery("from Student s where s.name.first = :name").setParameter("name", name).listResults();
}
public List findByEmail(String email)
{
return manager.createQuery("from Student s where s.email = :email").setParameter("email", email).listResults();
}
public void merge(Student s)
{
manager.merge(s);
}
}
Client.java
package com.kuaff.ejb3.composite;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.List;
public class Client
{
public static void main(String[] args) throws NamingException
{
InitialContext ctx = new InitialContext();
StudentDAO dao = (StudentDAO) ctx.lookup(StudentDAO.class.getName());
dao.create("晁","岳攀","8","[email protected]");
dao.create("朱","立焕","6","[email protected]");
Name name = new Name("朱","立焕");
//List list = dao.findByEmail("[email protected]");
Student s = dao.find(name);
System.out.printf("%s %s的email:%s%n",s.getName().getFirst(),s.getName().getLast(),s.getEmail());
/*
for(Object o:list)
{
Student s = (Student)o;
System.out.printf("%s %s的email:%s%n",s.getName().getFirst(),s.getName().getLast(),s.getEmail());
}
*/
}
}
这个客户端用来测试。
请运行{$JBOSS_HOME}/bin目录下的run.bat: run –c all,启动JBOSS。
http://localhost:8080/jmx-console/HtmlAdaptor?action=inspectMBean&name=jboss%3Aservice%3DHypersonic%2Cdatabase%3DlocalDB,然后调用startDatabaseManager()方法,打开HSQL管理工具管理数据库。
在Eclipse的Ant视图中执行ejbjar target。或者在命令行下,进入到此工程目录下,执行ant ejbjar,将编译打包发布此EJB。
在Eclipse的Ant视图中执行run target。或者在命令行下,进入到此工程目录下,执行ant run,测试这个EJB。
本文地址:http://com.8s8s.com/it/it12559.htm