在前面的所有的實體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=inspect