Fight With Me!!!

导航

JPA中的@MappedSuperclass

说明地址:http://docs.oracle.com/javaee/5/api/javax/persistence/MappedSuperclass.html

用来申明一个超类,继承这个类的子类映射时要映射此类中的字段,可以当做是对entity抽取封装的类。如果子类想重写此类的映射信息,可以使用 AttributeOverride and AssociationOverride annotations

 

Java代码  收藏代码
  1.  Example: Concrete class as a mapped superclass  
  2. @MappedSuperclass  
  3. public class Employee {  
  4.       
  5.         @Id   
  6.         protected Integer empId;  
  7.         @Version   
  8.         protected Integer version;  
  9.         @ManyToOne @JoinColumn(name="ADDR")  
  10.         protected Address address;  
  11.       
  12.         public Integer getEmpId() { ... }  
  13.         public void setEmpId(Integer id) { ... }  
  14.         public Address getAddress() { ... }  
  15.         public void setAddress(Address addr) { ... }  
  16. }  
  17.       
  18. // Default table is FTEMPLOYEE table  
  19. @Entity  
  20. public class FTEmployee extends Employee {  
  21.       
  22.         // Inherited empId field mapped to FTEMPLOYEE.EMPID  
  23.         // Inherited version field mapped to FTEMPLOYEE.VERSION  
  24.         // Inherited address field mapped to FTEMPLOYEE.ADDR fk  
  25.           
  26.     // Defaults to FTEMPLOYEE.SALARY  
  27.     protected Integer salary;  
  28.       
  29.     public FTEmployee() {}  
  30.       
  31.     public Integer getSalary() { ... }  
  32.       
  33.     public void setSalary(Integer salary) { ... }  
  34. }  
  35.       
  36.     @Entity @Table(name="PT_EMP")  
  37.     @AssociationOverride(name="address",   
  38.     joincolumns=@JoinColumn(name="ADDR_ID"))  
  39.     public class PartTimeEmployee extends Employee {  
  40.       
  41.         // Inherited empId field mapped to PT_EMP.EMPID  
  42.         // Inherited version field mapped to PT_EMP.VERSION  
  43.         // address field mapping overridden to PT_EMP.ADDR_ID fk  
  44.         @Column(name="WAGE")  
  45.         protected Float hourlyWage;  
  46.       
  47.         public PartTimeEmployee() {}  
  48.       
  49.         public Float getHourlyWage() { ... }  
  50.         public void setHourlyWage(Float wage) { ... }  
  51.     }  
  52.   
  53.     Example: Non-entity superclass  
  54.   
  55.     public class Cart {  
  56.       
  57.         // This state is transient  
  58.         Integer operationCount;  
  59.       
  60.         public Cart() { operationCount = 0; }  
  61.         public Integer getOperationCount() { return operationCount; }  
  62.         public void incrementOperationCount() { operationCount++; }  
  63.     }  
  64.       
  65.     @Entity  
  66.     public class ShoppingCart extends Cart {  
  67.       
  68.         Collection items = new Vector();  
  69.       
  70.         public ShoppingCart() { super(); }  
  71.       
  72.       
  73.     ...  
  74.       
  75.         @OneToMany  
  76.         public Collection getItems() { return items; }  
  77.         public void addItem(Item item) {  
  78.             items.add(item);  
  79.             incrementOperationCount();  
  80.         }  
  81.     }  
分享到:

posted on 2015-06-29 10:20  nickTimer  阅读(318)  评论(0编辑  收藏  举报