时间:2021-07-01 10:21:17 帮助过:12人阅读
在EJB3.0中,实体Bean可以实现继承关系。 比如有个Person的实体bean,它有姓名和性别两个属性。
上帝和女娲造人的时候,造出两种人:Man和Woman。Man和Woman都是实体Bean,而且他们都继承Person。
单一表策略就是副实体和子实体的数据都存放在一张表中,同时指定一列用来区别这些实体。
如:
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE, discriminatorType = DiscriminatorType.STRING)
@DiscriminatorColumn(name = "P_TYPE", nullable = true)
@ Inheritance的注释声明如下:
@ @Target({TYPE}) @Retention(RUNTIME)
public @interface Inheritance {
InheritanceType strategy() default SINGLE_TABLE;
DiscriminatorType discriminatorType() default STRING;
String discriminatorValue() default "";
}
这个注释用来指定继承使用的策略,以及为了区别这些实体所用的列的类型与值。
@DiscriminatorColumn注释用在单一表策略和联合表策略上。用来指定区别各实体所需的列。
@Target({TYPE}) @Retention(RUNTIME)
public @interface DiscriminatorColumn {
String name() default "";
boolean nullable() default false;
String columnDefinition() default "";
int length() default 10;
}
这个例子主要有以下几个文件,这个例子主要实现了Person和Man、Woman之间的继承关系,下面两章介绍的例子和这个例子相同。Man和Woman继承Person实体Bean。前面的例子一样,我们还是使用Client测试。
Person.java:实体Bean。
Man.java:实体Bean所依赖的类。
Woman.java:实体Bean所依赖的类。
PersonTest.java:会话Bean的业务接口
PersonTestBean.java:会话Bean的实现类
Client.java:测试EJB的客户端类。
jndi.properties:jndi属性文件,提供访问jdni的基本配置属性。
Build.xml:ant 配置文件,用以编译、发布、测试、清除EJB。
下面针对每个文件的内容做一个介绍。
Person.java
package com.kuaff.ejb3.singleinheritance;
import javax.ejb.DiscriminatorColumn;
import javax.ejb.DiscriminatorType;
import javax.ejb.Entity;
import javax.ejb.GeneratorType;
import javax.ejb.Id;
import javax.ejb.Inheritance;
import javax.ejb.InheritanceType;
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE, discriminatorType = DiscriminatorType.STRING)
@DiscriminatorColumn(name = "P_TYPE", nullable = true)
public class Person implements java.io.Serializable
{
private int id;
private String name;
private String gender;
@Id(generate = GeneratorType.AUTO)
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public void setGender(String gender)
{
this.gender = gender;
}
public String getGender()
{
return gender;
}
}
指定使用P_TYPE列用来区别各实体Bean。
Man.java
package com.kuaff.ejb3.singleinheritance;
import javax.ejb.DiscriminatorType;
import javax.ejb.Entity;
import javax.ejb.Inheritance;
import javax.ejb.InheritanceType;
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE, discriminatorType = DiscriminatorType.STRING, discriminatorValue = "Man")
public class Man extends Person
{
private boolean isGood;
public void setGood(boolean isGood)
{
this.isGood = isGood;
}
public boolean isGood()
{
return isGood;
}
}
这个实体Bean增加了一个是否是好男人的属性。
Woman.java
package com.kuaff.ejb3.singleinheritance;
import javax.ejb.DiscriminatorType;
import javax.ejb.Entity;
import javax.ejb.Inheritance;
import javax.ejb.InheritanceType;
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE, discriminatorType = DiscriminatorType.STRING, discriminatorValue = "Woman")
public class Woman extends Person
{
private boolean isbeautiful;
public void setIsbeautiful(boolean isbeautiful)
{
this.isbeautiful = isbeautiful;
}
public boolean isIsbeautiful()
{
return isbeautiful;
}
}
EntityTest.java
package com.kuaff.ejb3.singleinheritance;
import javax.ejb.Remote;
import java.util.List;
@Remote
public interface PersonDAO
{
public int createMan(String name,String gender,boolean b);
public int createWoman(String name,String gender,boolean b);
public Person find(int i);
public List findByName(String name);
public List findByInfo(String gender);
}
PersonTestBean.java
package com.kuaff.ejb3.singleinheritance;
import javax.ejb.EntityManager;
import javax.ejb.Inject;
import javax.ejb.Stateless;
import java.util.List;
@Stateless
public class PersonDAOBean implements PersonDAO
{
@Inject
private EntityManager manager;
public int createMan(String name,String gender,boolean b)
{
Man man = new Man();
man.setName(name);
man.setGender(gender);
man.setGood(b);
manager.create(man);
return man.getId();
}
public int createWoman(String name, String gender,boolean b)
{
Woman woman = new Woman();
woman.setName(name);
woman.setGender(gender);
woman.setIsbeautiful(b);
manager.create(woman);
return woman.getId();
}
public Person find(int i)
{
return manager.find(Person.class,i);
}
public List findByName(String name)
{
return manager.createQuery("from Person p where p.name =:name").setParameter("name", name).listResults();
}
public List findByInfo(String gender)
{
return manager.createQuery("from Person p where p.gender =:gender").setParameter("gender", gender).listResults();
}
}
在这个会话Bean中提供了创建Man、Woman实体Bean的方法,并提供了查找方法。
Client.java
package com.kuaff.ejb3.singleinheritance;
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();
PersonDAO dao = (PersonDAO) ctx.lookup(PersonDAO.class.getName());
int i = dao.createMan("晁岳攀","男",true);
dao.createWoman("朱立焕","女",true);
Person p = dao.find(i);
System.out.printf("%s的性别:%s%n",p.getName(),p.getGender());
List list = dao.findByName("朱立焕");
for (Object o:list)
{
Woman w = (Woman)o;
System.out.printf("%s漂亮吗?结论:%b%n",w.getName(),w.isIsbeautiful());
}
}
}
这个客户端用来测试。
请运行{$JBOSS_HOME}/bin目录下的run.bat: run