Example usage for javax.persistence AccessType FIELD

List of usage examples for javax.persistence AccessType FIELD

Introduction

In this page you can find the example usage for javax.persistence AccessType FIELD.

Prototype

AccessType FIELD

To view the source code for javax.persistence AccessType FIELD.

Click Source Link

Document

Field-based access is used.

Usage

From source file:org.guzz.builder.JPA2AnnotationsBuilder.java

protected static void parseClassForAttributes(GuzzContextImpl gf, POJOBasedObjectMapping map, Business business,
        DBGroup dbGroup, SimpleTable st, Class domainClass) {
    //???//  www.j  ava  2s. c o  m
    Class parentCls = domainClass.getSuperclass();
    if (parentCls != null && parentCls.isAnnotationPresent(MappedSuperclass.class)) {
        parseClassForAttributes(gf, map, business, dbGroup, st, parentCls);
    }

    javax.persistence.Access access = (javax.persistence.Access) domainClass
            .getAnnotation(javax.persistence.Access.class);
    AccessType accessType = null;

    if (access == null) {
        //@Id@Idfieldproperty
        boolean hasColumnAOnField = false;
        boolean hasColumnAOnProperty = false;

        //detect from @Id, field first.
        Field[] fs = domainClass.getDeclaredFields();

        for (Field f : fs) {
            if (f.isAnnotationPresent(Transient.class))
                continue;
            if (f.isAnnotationPresent(javax.persistence.Id.class)) {
                accessType = AccessType.FIELD;
                break;
            } else if (f.isAnnotationPresent(javax.persistence.Column.class)) {
                hasColumnAOnField = true;
            } else if (f.isAnnotationPresent(org.guzz.annotations.Column.class)) {
                hasColumnAOnField = true;
            }
        }

        if (accessType == null) {
            Method[] ms = domainClass.getDeclaredMethods();
            for (Method m : ms) {
                if (m.isAnnotationPresent(Transient.class))
                    continue;
                if (m.isAnnotationPresent(javax.persistence.Id.class)) {
                    accessType = AccessType.PROPERTY;
                    break;
                } else if (m.isAnnotationPresent(javax.persistence.Column.class)) {
                    hasColumnAOnProperty = true;
                } else if (m.isAnnotationPresent(org.guzz.annotations.Column.class)) {
                    hasColumnAOnProperty = true;
                }
            }
        }

        //@Id@Column@Columnfield?
        if (accessType == null) {
            if (hasColumnAOnField) {
                accessType = AccessType.FIELD;
            } else if (hasColumnAOnProperty) {
                accessType = AccessType.PROPERTY;
            } else {
                accessType = AccessType.FIELD;
            }
        }
    } else {
        accessType = access.value();
    }

    //orm by field
    if (accessType == AccessType.FIELD) {
        Field[] fs = domainClass.getDeclaredFields();

        for (Field f : fs) {
            if (f.isAnnotationPresent(Transient.class))
                continue;
            if (Modifier.isTransient(f.getModifiers()))
                continue;
            if (Modifier.isStatic(f.getModifiers()))
                continue;

            if (f.isAnnotationPresent(javax.persistence.Id.class)) {
                addIdMapping(gf, map, st, dbGroup, f.getName(), domainClass, f);
            } else {
                addPropertyMapping(gf, map, st, f.getName(), f, f.getType());
            }
        }
    } else {
        Method[] ms = domainClass.getDeclaredMethods();
        for (Method m : ms) {
            if (m.isAnnotationPresent(Transient.class))
                continue;
            if (Modifier.isTransient(m.getModifiers()))
                continue;
            if (Modifier.isStatic(m.getModifiers()))
                continue;
            if (Modifier.isPrivate(m.getModifiers()))
                continue;

            String methodName = m.getName();
            String fieldName = null;

            if (m.getParameterTypes().length != 0) {
                continue;
            } else if (Void.TYPE.equals(m.getReturnType())) {
                continue;
            }

            if (methodName.startsWith("get")) {
                fieldName = methodName.substring(3);
            } else if (methodName.startsWith("is")) {//is boolean?
                Class retType = m.getReturnType();

                if (boolean.class.isAssignableFrom(retType)) {
                    fieldName = methodName.substring(2);
                } else if (Boolean.class.isAssignableFrom(retType)) {
                    fieldName = methodName.substring(2);
                }
            }

            //not a javabean read method
            if (fieldName == null) {
                continue;
            }

            fieldName = java.beans.Introspector.decapitalize(fieldName);

            if (m.isAnnotationPresent(javax.persistence.Id.class)) {
                addIdMapping(gf, map, st, dbGroup, fieldName, domainClass, m);
            } else {
                addPropertyMapping(gf, map, st, fieldName, m, m.getReturnType());
            }
        }
    }

    //?attribute override
    AttributeOverride gao = (AttributeOverride) domainClass.getAnnotation(AttributeOverride.class);
    AttributeOverrides gaos = (AttributeOverrides) domainClass.getAnnotation(AttributeOverrides.class);
    AttributeOverride[] aos = gao == null ? new AttributeOverride[0] : new AttributeOverride[] { gao };
    if (gaos != null) {
        ArrayUtil.addToArray(aos, gaos.value());
    }

    for (AttributeOverride ao : aos) {
        String name = ao.name();
        Column col = ao.column();

        TableColumn tc = st.getColumnByPropName(name);
        Assert.assertNotNull(tc,
                "@AttributeOverride cann't override a attribute that doesn't exist. The attribute is:" + name);

        //update is remove and add
        st.removeColumn(tc);

        //change the column name in the database.
        tc.setColName(col.name());

        st.addColumn(tc);
    }
}