public static String getEntityName(Class entity) { if (mappedName.get(entity) != null) { return mappedName.get(entity); } String name = null; Table table = (Table) entity.getAnnotation(Table.class); if (table != null && table.name() != null && !table.name().isEmpty()) { name = table.name(); } else { Entity entityAnnotation = (Entity) entity.getAnnotation(Entity.class); if (entityAnnotation != null && entityAnnotation.name() != null && !entityAnnotation.name().isEmpty()) { name = entityAnnotation.name(); } else { name = entity.getSimpleName(); } } mappedName.put(entity, name); return name; }
/** * Gets all the annotation info for a particular class and puts it in our annotation info cache. * * @param c * @return */ public AnnotationInfo putAnnotationInfo(Class c) { { Entity entity = (Entity) c.getAnnotation(Entity.class); if (entity == null) { throw new PersistenceException("Class not marked as an @Entity: " + c.getName()); } } AnnotationInfo ai = new AnnotationInfo(); ai.setClassAnnotations(c.getAnnotations()); ai.setMainClass(c); Class superClass = c; Class rootClass = null; while ((superClass = superClass.getSuperclass()) != null) { MappedSuperclass mappedSuperclass = (MappedSuperclass) superClass.getAnnotation(MappedSuperclass.class); Entity entity = (Entity) superClass.getAnnotation(Entity.class); Inheritance inheritance = (Inheritance) superClass.getAnnotation(Inheritance.class); if (mappedSuperclass != null || entity != null) { putProperties(ai, superClass); putMethods(ai, superClass); if (entity != null) { rootClass = superClass; } putEntityListeners(ai, superClass); } } if (rootClass != null) { ai.setRootClass(rootClass); DiscriminatorValue dv = (DiscriminatorValue) c.getAnnotation(DiscriminatorValue.class); String discriminatorValue; if (dv != null) { discriminatorValue = dv.value(); if (discriminatorValue == null) { throw new PersistenceException( "You must specify a value= for @DiscriminatorValue on " + c.getName()); } } else { discriminatorValue = c.getSimpleName(); } ai.setDiscriminatorValue(discriminatorValue); discriminatorMap.put(discriminatorValue, ai); } else { ai.setRootClass(c); } putTableDeclaration(ai, c); putProperties(ai, c); putMethods(ai, c); if (ai.getIdMethod() == null) { throw new PersistenceException("No ID method specified for: " + c.getName()); } putEntityListeners(ai, c); getAnnotationMap().put(c.getName(), ai); return ai; }
public List<E> findAll() { EntityManager em = getEntityManager(); EntityTransaction tx = em.getTransaction(); List<E> list = new ArrayList<E>(); try { tx.begin(); Query q = em.createNamedQuery(clazz.getSimpleName() + ".findAll"); list = q.getResultList(); tx.commit(); } finally { if (tx.isActive()) { tx.rollback(); } em.close(); } return list; }
public <T> List<T> list(Class<T> target) { return (List<T>) entityManager.createQuery("SELECT t FROM " + target.getSimpleName() + " t").getResultList(); }