Example usage for javax.persistence.metamodel EntityType hasSingleIdAttribute

List of usage examples for javax.persistence.metamodel EntityType hasSingleIdAttribute

Introduction

In this page you can find the example usage for javax.persistence.metamodel EntityType hasSingleIdAttribute.

Prototype

boolean hasSingleIdAttribute();

Source Link

Document

Whether the identifiable type has a single id attribute.

Usage

From source file:org.apache.click.extras.jpa.JpaForm.java

/**
 * Create a new JpaForm with the given form name and value object
 * class.//from w w w. j  a v a2  s  .  co  m
 *
 * @param name the form name
 * @param valueClass the value object class
 */
public JpaForm(String name, Class valueClass) {
    super(name);

    if (valueClass == null) {
        throw new IllegalArgumentException("Null valueClass parameter");
    }

    classField = new HiddenField(FO_CLASS, String.class);
    classField.setValue(valueClass.getName());
    add(classField);

    /*
    String classname = getClassname(valueClass);
            
    ClassMetadata classMetadata =
    ((HibernateEntityManagerFactory)getEntityManagerFactory()).getSessionFactory().getClassMetadata(classname);
            
    Type identifierType = classMetadata.getIdentifierType();
    oidField = new HiddenField(FO_ID, identifierType.getReturnedClass());
    add(oidField);
     */

    Metamodel classMetadata = getEntityManager().getMetamodel();
    EntityType entityType = classMetadata.entity(valueClass);
    //Whether the identifiable type has a single id attribute.
    if (entityType.hasSingleIdAttribute()) {
        oidField = new HiddenField(FO_ID, entityType.getIdType().getJavaType());
    } else {
        throw new IllegalArgumentException("The identifiable type is idclass attribute, the form won't work!");
    }
    add(oidField);

}

From source file:org.apache.openejb.util.proxy.QueryProxy.java

private void remove(final Object[] args, final Class<?> returnType) {
    if (args != null && args.length == 1 && returnType.equals(Void.TYPE)) {
        Object entity = args[0];//  www  .  ja  va2s.  c  o  m
        if (!em.contains(entity)) { // reattach the entity if possible
            final Class<?> entityClass = entity.getClass();
            final EntityType<? extends Object> et = em.getMetamodel().entity(entityClass);

            if (!et.hasSingleIdAttribute()) {
                throw new IllegalArgumentException("Dynamic EJB doesn't manage IdClass yet");
            }

            SingularAttribute<?, ?> id = null; // = et.getId(entityClass); doesn't work with openJPA
            for (final SingularAttribute<?, ?> sa : et.getSingularAttributes()) {
                if (sa.isId()) {
                    id = sa;
                    break;
                }
            }
            if (id == null) {
                throw new IllegalArgumentException("id field not found");
            }
            final String idName = id.getName();

            final Object idValue;
            try {
                idValue = BeanUtils.getProperty(entity, idName);
            } catch (final InvocationTargetException e) {
                throw new IllegalArgumentException("can't invoke to get entity id");
            } catch (final NoSuchMethodException e) {
                throw new IllegalArgumentException("can't find the method to get entity id");
            } catch (final IllegalAccessException e) {
                throw new IllegalArgumentException("can't access field/method to get entity id");
            }

            entity = em.getReference(et.getJavaType(), idValue);
            if (entity == null) {
                throw new IllegalArgumentException("entity " + entity + " is not managed and can't be found.");
            }
        }
        em.remove(entity);
    } else {
        throw new IllegalArgumentException(REMOVE_NAME + " should have only one parameter and return void");
    }
}