JPA反射机制实现对象获取-1

private static final Log log = LogFactory.getLog(JPASupport.class);
   
    private static final long serialVersionUID = 1L;
   
    /*
     * 用于存放POJO的列信息
     */
    private transient static Map<Class<? extends JPASupport>, List<String>> columnMap = new HashMap<Class<? extends JPASupport>, List<String>>();
   
    /**
     * 获取POJO对应的表名
     * 需要POJO中的属性定义  @Table(name)
     *
     * @return String
     */
    public String tableName()
    {
        log.debug("loading JAPSupport method tableName..");
       
        Table table = this.getClass().getAnnotation(Table.class);
       
        if (null != table)
        {
            return table.name();
        }
        else
        {
            throw new RuntimeException("undefine POJO @Table, need Tablename(@Table)..");
        }
    }
   
    /**
     * 获取POJO对应的主键名称
     * 需要POJO中的属性定义  @Id
     *
     * @return String
     */
    public String id()
    {
        log.debug("loading JAPSupport method id..");
        log.debug(this.getClass().getName());
       
        for (Field field : this.getClass().getDeclaredFields())
        {
            if (field.isAnnotationPresent(Id.class))
            {
                return field.getName();
            }
        }
       
        throw new RuntimeException("undefine POJO @Id..");
    }
   
    /**
     * 用于计算类定义
     * 需要POJO中的属性定义  @Column(name)
     */
    public void caculationColumnList()
    {
        if (columnMap.containsKey(this.getClass()))
        {
            return;
        }
       
        Field[] fields = this.getClass().getDeclaredFields();
        List<String> columnList = new ArrayList<String>(fields.length);
       
        for (Field field : fields)
        {
            if (field.isAnnotationPresent(Column.class))
            {
                columnList.add(field.getName());
            }
        }
       
        columnMap.put(this.getClass(), columnList);
    }
   
    /**
     * 获取用于WHERE的 有值字段表
     * @return List
     */
    public List<WhereColumn> returnWhereColumnsName()
    {
        Field[] fields = this.getClass().getDeclaredFields();
        List<WhereColumn> columnList = new ArrayList<WhereColumn>(fields.length);
       
        for (Field field : fields)
        {
            if (field.isAnnotationPresent(Column.class) && !isNull(field))
            {
                columnList.add(new WhereColumn(field.getName(),field.getGenericType().equals(String.class)));
            }
        }
       
        return columnList;
    }
   
    /**
     * Where条件信息
     */
    public class WhereColumn
    {
        public String name;
       
        public boolean isString;
       
        public WhereColumn(String name, boolean isString)
        {
            this.name = name;
            this.isString = isString;
        }
    }
   
    /**
     * 用于获取Insert的字段累加
     * @return String
     */
    public String returnInsertColumnsName()
    {
        StringBuilder sb = new StringBuilder();
       
        List<String> list = columnMap.get(this.getClass());
        int i = 0;
       
        for (String column : list)
        {
            if (isNull(column))
            {
                continue;
            }
           
            if (i++ != 0)
            {
                sb.append(',');
            }
           
            sb.append(column);
        }
       
        return sb.toString();
    }

你可能感兴趣的:(jpa)