get Table Name from Annotation - Java java.lang.annotation

Java examples for java.lang.annotation:Enterprise Annotation

Description

get Table Name from Annotation

Demo Code


import javax.persistence.Entity;
import javax.persistence.Table;

public class Main{
    public static void main(String[] argv) throws Exception{
        Class clazz = String.class;
        System.out.println(getTableName(clazz));
    }/*w w  w .  java2 s.co m*/
    public static String getTableName(Class<?> clazz) {
        Table ann = clazz.getAnnotation(Table.class);
        if (ann != null && !"".equals(ann.name())) {
            return ann.name();
        }
        return getEntityName(clazz);
    }
    public static String getEntityName(Class<?> clazz) {
        Entity ann = clazz.getAnnotation(Entity.class);
        if (ann != null && !"".equals(ann.name())) {
            return ann.name();
        }
        return clazz.getSimpleName();
    }
}

Related Tutorials