日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/3 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 声明jpa批注处理器_如何使用反射基于JPA批注记录您的数据模型 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

聲明jpa批注處理器

因此,當您僅可以注釋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批注處理器

總結

以上是生活随笔為你收集整理的声明jpa批注处理器_如何使用反射基于JPA批注记录您的数据模型的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。