예제 #1
0
  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;
  }
예제 #2
0
  /**
   * 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;
  }
예제 #3
0
 private void putEntityListeners(AnnotationInfo ai, Class c) {
   EntityListeners listeners = (EntityListeners) c.getAnnotation(EntityListeners.class);
   if (listeners != null) {
     logger.fine("Found EntityListeners for " + c + " - " + listeners);
     putEntityListeners(ai, listeners);
   }
 }
예제 #4
0
  private void putTableDeclaration(AnnotationInfo ai, Class<?> c) {
    Table table = c.getAnnotation(Table.class);
    if (table != null) {
      if (table.name() == null)
        throw new PersistenceException("You must specify a name= for @Table on " + c.getName());

      ai.setDomainName(table.name());
    }
  }
예제 #5
0
파일: JPAPlugin.java 프로젝트: visan/play1
 private Class<?> getCompositeKeyClass() {
   Class<?> tclazz = clazz;
   while (!tclazz.equals(Object.class)) {
     // Only consider mapped types
     if (tclazz.isAnnotationPresent(Entity.class)
         || tclazz.isAnnotationPresent(MappedSuperclass.class)) {
       IdClass idClass = tclazz.getAnnotation(IdClass.class);
       if (idClass != null) return idClass.value();
     }
     tclazz = tclazz.getSuperclass();
   }
   throw new UnexpectedException(
       "Invalid mapping for class " + clazz + ": multiple IDs with no @IdClass annotation");
 }