ends 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();
}
}
EJB 3.0开发指南之实体Bean的继承(3)
时间:2009-11-27 天极
在这个会话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&qu
|