EJB 3.0开拓指南之组合主键
当前位置:以往代写 > JAVA 教程 >EJB 3.0开拓指南之组合主键
2019-06-14

EJB 3.0开拓指南之组合主键

EJB 3.0开拓指南之组合主键

副标题#e#

在前面的所有的实体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;
}
}


#p#副标题#e#

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

#p#分页标题#e#

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。

    关键字:

在线提交作业