分类:
2008-11-17 22:15:58
@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; } |
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; } } |
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; } } |
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; } } |
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); } |
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(); } } |
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()); } } } |