因此,当您仅可以注释Java类时,使用JPA,Hibernate或EBeans会很酷,但是您不是一直希望可以从代码“生成”数据模型的文档吗? 提取JPA / Hibernate和其他验证批注的信息?

假设您的bean中包含所有这些漂亮的注释:

@Entity
@Table(name = "project_bills")
public class Bill extends Model {private static final long serialVersionUID = 1L;@Id@Column(name="PBI_ID")public Long id;@DoubleFormat@Column(name="PBI_BILL_AMOUNT",length=22)public Double billAmount;@Column(name="PBI_BILL_DATE")@DateTime(pattern="dd.MM.yyyy")public Date billDate;@Column(name="PBI_BILL_NUMBER",length=10)public String billNumber;@Column(name="PBI_CAN_BILL")public Boolean canBill;@Column(name="PBI_COMMENT",length=65535)public String comment;@Column(name="PBI_PAID_DATE")@DateTime(pattern="dd.MM.yyyy")public Date paidDate;

这是如何完成该任务的示例:

public static String listEntities(String _package) {StringBuffer retval = new StringBuffer();Reflections reflections = new Reflections(_package, Play.application().classloader());Set<Class<?>> classes = reflections.getTypesAnnotatedWith(javax.persistence.Entity.class);for (Class c : classes) {retval.append(c.getName() + "\n");retval.append(listAnnotations(c.getName()) + "\n\n");retval.append(listAttributes(c.getName()) + "\n\n");}return retval.toString();}public static String listAnnotations(String _class) {StringBuffer retval = new StringBuffer();try {Annotation[] annotations = Class.forName(_class).getAnnotations();if (annotations.length != 0) {for (int j = 0; j < annotations.length; j++) {retval.append(annotations[j].toString() + ": " + annotations[j].annotationType() + "\n");}retval.append("\n");}} catch (ClassNotFoundException e) {System.out.println(e.toString());return retval.toString();}return retval.toString();}public static String listAttributes(String _class) {StringBuffer retval2 = new StringBuffer();boolean perstistent = false;try {for (Field field : Class.forName(_class).getDeclaredFields()) {Class type = field.getType();String name = field.getName();perstistent = false;StringBuffer retval = new StringBuffer();retval.append("\t" + name + " (" + type + ")\n");Annotation[] annotations = field.getDeclaredAnnotations();if (annotations.length != 0) {for (int j = 0; j < annotations.length; j++) {retval.append(annotations[j].toString() + ": " + annotations[j].annotationType() + "\n");if (annotations[j].toString().startsWith("@javax.persistence")) {perstistent = true;}}retval.append("\n");}if (perstistent) {retval2.append(retval);}}} catch (ClassNotFoundException e) {System.out.println(e.toString());return retval2.toString();}return retval2.toString();}

这将生成如下内容:

models.controlling.Bill
@javax.persistence.Table(schema=, uniqueConstraints=[], catalog=, name=project_bills): interface javax.persistence.Table
@javax.persistence.Entity(name=): interface javax.persistence.Entityid (class java.lang.Long)
@javax.persistence.Id(): interface javax.persistence.Id
@javax.persistence.Column(insertable=true, scale=0, unique=false, precision=0, columnDefinition=, name=PBI_ID, updatable=true, length=255, nullable=true, table=): interface javax.persistence.ColumnbillAmount (class java.lang.Double)
@utils.data.formatters.Formats$DoubleFormat(): interface utils.data.formatters.Formats$DoubleFormat
@javax.persistence.Column(insertable=true, scale=0, unique=false, precision=0, columnDefinition=, name=PBI_BILL_AMOUNT, updatable=true, length=22, nullable=true, table=): interface javax.persistence.ColumnbillDate (class java.util.Date)
@javax.persistence.Column(insertable=true, scale=0, unique=false, precision=0, columnDefinition=, name=PBI_BILL_DATE, updatable=true, length=255, nullable=true, table=): interface javax.persistence.Column
@play.data.format.Formats$DateTime(pattern=dd.MM.yyyy): interface play.data.format.Formats$DateTimebillNumber (class java.lang.String)
@javax.persistence.Column(insertable=true, scale=0, unique=false, precision=0, columnDefinition=, name=PBI_BILL_NUMBER, updatable=true, length=10, nullable=true, table=): interface javax.persistence.Column

当然,这只是冰山一角,但是您明白了。

参考: 如何使用 Poornerd博客上的JCG合作伙伴 Brian Porter的JPA注释 , 如何使用反射来记录数据模型 。

翻译自: https://www.javacodegeeks.com/2013/07/how-to-use-reflection-to-document-your-data-model-based-on-jpa-annotations.html

如何使用反射来基于JPA注释记录数据模型相关推荐

  1. 声明jpa批注处理器_如何使用反射基于JPA批注记录您的数据模型

    声明jpa批注处理器 因此,当您仅可以注释Java类时,使用JPA,Hibernate或EBeans很酷,但是您不是一直希望可以从代码"生成"数据模型的文档吗? 提取JPA / H ...

  2. 基于JPA实现多表连接查询

    基于 JPA 显示城市信息 一.JPA JPA 定义 二.任务 实战 1.项目结构 2.项目配置信息 3.实体 4.映射 5.控制器 6.UI 三.效果图 一.JPA JPA 定义 Java 持久化 ...

  3. java 注释 连接,java – 如何使用JPA注释创建连接表?

    我需要使用JPA注释在我的数据库中创建一个连接表,结果将是这样的: 到目前为止,我刚刚实现了两个实体: @Entity @Table(name="USERS", schema=&q ...

  4. oracle jpa字段加注释,JPA注释参考手册.doc

    JPA注释参考手册 JPA注释参考手册(转)?? Table Table用来定义entity主表的name,catalog,schema等属性. 元数据属性说明: name: 表名 catalog: ...

  5. 租户隔离怎么做MYSQL_基于JPA实现SaaS多租户模式的数据存储——共享数据库,隔离数据架构...

    SaaS是Software-as-a-Service(软件即服务)的简称,这边具体的解释不介绍.多租户的系统可以应用这种模式的思想,将思想融入到系统的设计之中. 现在SaaS Multi-Tenant ...

  6. XDoc 基于Java注释生成API文档

    XDoc 基于Java注释生成API文档 <!--加入maven依赖--> <dependency><groupId>com.github.treeleafj< ...

  7. 【Python文本处理】基于运动路线记录GPX文件解析,心率、速度、时间、功率、踏频、海拔等参数的生成和更改,以及GPX循环拼接

    [Python文本处理]基于运动路线记录GPX文件解析,心率.速度.时间.功率.踏频.海拔等参数的生成和更改,以及GPX循环拼接 GPX文件本身其实就是坐标.海拔.时间.心率等综合性的xml文件 如图 ...

  8. 【Python文本处理】基于运动路线记录GPX的文件解析,及对经纬度坐标的数学模型运动速度求解

    [Python文本处理]基于运动路线记录GPX的文件解析,及对经纬度坐标的数学模型运动速度求解 解析 GPX文件格式 GPX文件本身其实就是坐标.海拔.时间.心率等综合性的xml文件 如图: 海拔:e ...

  9. 【Python文本处理】基于运动路线记录GPX文件的基础运动速度求解,并转为SRT字幕格式(不需要安装三方库)

    [Python文本处理]基于运动路线记录GPX文件的基础运动速度求解,并转为SRT字幕格式(不需要安装三方库) 解析 GPX文件格式 GPX文件本身其实就是坐标.海拔.时间.心率等综合性的xml文件 ...

最新文章

  1. PostgreSQL中的大容量空间探索时间序列数据存储
  2. 微型计算机外文文献,电子信息科学与技术专业Microcomputer-Systems微型计算机控制系统大学毕业论文外文文献翻译及原文.doc...
  3. 部署eureka和config
  4. verilog 给数组集体赋值_Verilog中如何对数组赋值(存储器memory详解)
  5. TCP/IP 体系结构
  6. 漫步凸分析八——回收锥与无界
  7. Oracle occi 抓包,instantclient 32下载-instantclient-basic(轻量级数据库)32位 11.2.0.4.0 官方版 - 河东下载站...
  8. linux恢复fat文件系统,使用‘fsck’修复Linux中文件系统错误的方法
  9. Java编程经典10道_10道经典java编程笔试题
  10. 计算机招聘网站排名,2014年互联网名企招聘人数的高校
  11. 差分pid模块_基于数字PID切换控制的Buck变换器研究
  12. [数据库】sql 查询语句 汇总
  13. 为什么要在信号线上串联一些小电阻
  14. 浅谈大数据时代web数据可视化探析
  15. Word2016文档完美地在表格里的方框(□)中打钩(√)
  16. 转 一个游戏程序员的学习资料
  17. iphone各版本分辨率
  18. 指定JDK运行Jar包
  19. ae 创建图像等高线 蒙版_「PS软件」工具使用,图层蒙版与橡皮擦的应用分析
  20. 【PyTorch】提高mnist模型精确度

热门文章

  1. 第5步 配置pom.xml文件 pom文件好了就是jar包引入好了
  2. wxml报错原因_C#生成、解析xml文件以及处理报错原因
  3. rabbitmq-死信队列
  4. 数据库表连接总结:等值连接, 自然连接,左外连接,右外连接,内连接,全外连接;
  5. 反射和配置文件的实际应用
  6. Spring boot(3):Spring boot中Redis 的使用
  7. 代码大全和新月神话_神话般的代码
  8. 分布式 虚拟时间和虚拟同步_分布式虚拟跟踪
  9. java 方法 示例_Java 9示例–收集的工厂方法–创建不可修改的列表,集合和映射...
  10. java登录界面命令_Java命令行界面(第11部分):CmdLn