• 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.     }  
    分享到:
  • 相关阅读:
    OC闪屏页尺寸
    OC 应用跳转QQ私聊界面或者申请加群
    一款好用的相册浏览器
    获取UILabel的numberOfLine
    一、spring入门案例
    三、为什么要配置环境变量?怎么用cmd编译运行java代码?
    十三、Swing绘图技术
    十二、泛型、反射和异常
    十一、集合框架
    十、二进制、位运算、位移运算符
  • 原文地址:https://www.cnblogs.com/toSeeMyDream/p/4606896.html
Copyright © 2020-2023  润新知