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);
}
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();
}
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 –c all,启动JBOSS。