说明地址: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. @Id
  5. protected Integer empId;
  6. @Version
  7. protected Integer version;
  8. @ManyToOne @JoinColumn(name="ADDR")
  9. protected Address address;
  10. public Integer getEmpId() { ... }
  11. public void setEmpId(Integer id) { ... }
  12. public Address getAddress() { ... }
  13. public void setAddress(Address addr) { ... }
  14. }
  15. // Default table is FTEMPLOYEE table
  16. @Entity
  17. public class FTEmployee extends Employee {
  18. // Inherited empId field mapped to FTEMPLOYEE.EMPID
  19. // Inherited version field mapped to FTEMPLOYEE.VERSION
  20. // Inherited address field mapped to FTEMPLOYEE.ADDR fk
  21. // Defaults to FTEMPLOYEE.SALARY
  22. protected Integer salary;
  23. public FTEmployee() {}
  24. public Integer getSalary() { ... }
  25. public void setSalary(Integer salary) { ... }
  26. }
  27. @Entity @Table(name="PT_EMP")
  28. @AssociationOverride(name="address",
  29. joincolumns=@JoinColumn(name="ADDR_ID"))
  30. public class PartTimeEmployee extends Employee {
  31. // Inherited empId field mapped to PT_EMP.EMPID
  32. // Inherited version field mapped to PT_EMP.VERSION
  33. // address field mapping overridden to PT_EMP.ADDR_ID fk
  34. @Column(name="WAGE")
  35. protected Float hourlyWage;
  36. public PartTimeEmployee() {}
  37. public Float getHourlyWage() { ... }
  38. public void setHourlyWage(Float wage) { ... }
  39. }
  40. Example: Non-entity superclass
  41. public class Cart {
  42. // This state is transient
  43. Integer operationCount;
  44. public Cart() { operationCount = 0; }
  45. public Integer getOperationCount() { return operationCount; }
  46. public void incrementOperationCount() { operationCount++; }
  47. }
  48. @Entity
  49. public class ShoppingCart extends Cart {
  50. Collection items = new Vector();
  51. public ShoppingCart() { super(); }
  52. ...
  53. @OneToMany
  54. public Collection getItems() { return items; }
  55. public void addItem(Item item) {
  56. items.add(item);
  57. incrementOperationCount();
  58. }
  59. }
分享到:

转载于:https://www.cnblogs.com/toSeeMyDream/p/4606896.html

JPA中的@MappedSuperclass相关推荐

  1. java实体外键_如何在JPA中添加用户实体和好友关系实体的外键

    现有用户实体,相关代码如下: @Entity @Table(name = "t_users") public class User extends Model { @Id @Col ...

  2. jpa中使用Query判断条件查询

    jpa中使用Query判断条件查询 @Query(value = " select m.* from mining_area as m " +" where 1 = 1& ...

  3. 解决Springboot+JPA中多表关联查询会查询多次的问题(n+1查询问题)

    解决Springboot+JPA中多表关联查询会查询多次的问题(n+1查询问题) 参考文章: (1)解决Springboot+JPA中多表关联查询会查询多次的问题(n+1查询问题) (2)https: ...

  4. JPA中实现双向多对多的关联关系(附代码下载)

    场景 JPA入门简介与搭建HelloWorld(附代码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103473937 ...

  5. JPA中实现双向一对一的关联关系

    场景 JPA入门简介与搭建HelloWorld(附代码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103473937 ...

  6. JPA中实现双向一对多的关联关系

    场景 JPA入门简介与搭建HelloWorld(附代码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103473937 ...

  7. JPA中实现单向一对多的关联关系

    场景 JPA入门简介与搭建HelloWorld(附代码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103473937 ...

  8. Spring Boot JPA 中transaction的使用

    文章目录 @Transactional的实现 @Transactional的使用 Transaction的传播级别 REQUIRED SUPPORTS MANDATORY NEVER NOT_SUPP ...

  9. Spring Boot JPA中关联表的使用

    文章目录 添加依赖 构建Entity 构建Repository 构建初始数据 测试 Spring Boot JPA中关联表的使用 本文中,我们会将会通过一个Book和Category的关联关系,来讲解 ...

最新文章

  1. idea中一个maven项目增加Web支持
  2. 149. Leetcode 1005. K 次取反后最大化的数组和 (贪心算法-基础题目)
  3. hadoop_namenode如果选择在哪个datanode存储副本
  4. 还贷的那些事I——还贷的基本概念
  5. vue与php接口对接,怎样使用vue项目中api接口
  6. linux之安装mysql提示Error: Unable to find a match: mysql-community-server
  7. python 实现点击右键用某个程序打开功能_工欲善其事,必先利其器(1)——Python开发环境安装与配置...
  8. 100亿估值的文和友,为何难以“走出”长沙?
  9. 20200922:leetcode35周双周赛题解记录(下)
  10. 安装 jupyter notebook
  11. python生成矢量图_Jupyter Notebook输出矢量图实例
  12. 21. 总是让比较函数在等值情况下返回false
  13. 推荐装机软件更新下载(2006.11.7更新)
  14. springboot中使用thymeleaf片段引入出现500错误(易错)
  15. Eason's concert
  16. 图集 | 航拍玉林29所学校,里面有你的母校吗?
  17. 【摸鱼神器】— 五子棋
  18. 李白关于鸿蒙的诗,李白6首山水古诗,首首美到让人心碎
  19. 【深入理解RabbitMQ原理】RabbitMQ 配置,生产者和消费者
  20. 2021.1.30课程摘要(逻辑教育-王劲胜)

热门文章

  1. Thinkphp 配置不用输入index.php
  2. zabbix4.0 使用nginx前端安装
  3. Codeforces Round #201 (Div. 2)C,E
  4. 12.18栈、队列练习题
  5. LintCode 两两交换链表中的节点
  6. 系统地学习JavaScript
  7. 基于Response的将数据导出到Excel
  8. linux火狐浏览器49.0安装教程,火狐浏览器Linux最新版下载
  9. Python爬虫自学之第(①)篇——爬虫伪装和反“反爬”
  10. 支付宝支付—沙箱环境使用