如何使用反射来基于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.Entity

	id (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.Column

	billAmount (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.Column

	billDate (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$DateTime

	billNumber (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

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


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值