Example usage for javax.persistence GenerationType AUTO

List of usage examples for javax.persistence GenerationType AUTO

Introduction

In this page you can find the example usage for javax.persistence GenerationType AUTO.

Prototype

GenerationType AUTO

To view the source code for javax.persistence GenerationType AUTO.

Click Source Link

Document

Indicates that the persistence provider should pick an appropriate strategy for the particular database.

Usage

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

protected static void addIdMapping(GuzzContextImpl gf, POJOBasedObjectMapping map, SimpleTable st,
        DBGroup dbGroup, String name, Class domainClas, AnnotatedElement element) {
    javax.persistence.Column pc = (javax.persistence.Column) element
            .getAnnotation(javax.persistence.Column.class);
    org.guzz.annotations.Column gc = (org.guzz.annotations.Column) element
            .getAnnotation(org.guzz.annotations.Column.class);

    String type = gc == null ? null : gc.type();
    String column = pc == null ? null : pc.name();

    if (StringUtil.isEmpty(column)) {
        column = name;/*from w ww  . j a v a 2s. co  m*/
    }

    TableColumn col = st.getColumnByPropName(name);
    boolean newId = false;

    if (col == null) {
        newId = true;
        col = new TableColumn(st);
    } else {
        log.info("override @Id in the parent class of [" + st.getBusinessName() + "].");
    }

    col.setColName(column);
    col.setPropName(name);
    col.setType(type);
    col.setAllowInsert(true);
    col.setAllowUpdate(true);
    col.setLazy(false);
    map.initColumnMapping(col, null);

    if (newId) {
        st.addPKColumn(col);
    }

    //@Id generator
    javax.persistence.GeneratedValue pgv = (javax.persistence.GeneratedValue) element
            .getAnnotation(javax.persistence.GeneratedValue.class);
    if (pgv == null) {
        pgv = (javax.persistence.GeneratedValue) domainClas
                .getAnnotation(javax.persistence.GeneratedValue.class);
    }

    //If @GeneratedValue is not defined, use auto.
    GenerationType gt = GenerationType.AUTO;
    String generatorName = null;

    if (pgv != null) {
        gt = pgv.strategy();
        generatorName = pgv.generator();
    }

    Properties idProperties = new Properties();
    String igCls;

    if (gt == GenerationType.AUTO) {
        //??guzz@GenericGenerator
        if (StringUtil.notEmpty(generatorName)) {
            GenericGenerator ggg = (GenericGenerator) element.getAnnotation(GenericGenerator.class);
            if (ggg != null && !generatorName.equals(ggg.name())) {
                ggg = null;
            }

            if (ggg == null) {
                //retreive @Id from GlobalContext
                Object g = gf.getGlobalIdGenerator(generatorName);

                //should be GenericGenerator
                if (!(g instanceof GenericGenerator)) {
                    throw new IllegalParameterException("The Id Generator [" + generatorName
                            + "] should be of type @org.guzz.annotations.GenericGenerator. domain class:"
                            + domainClas.getName());
                }

                ggg = (GenericGenerator) g;
            }

            igCls = ggg.strategy();
            Parameter[] ps = ggg.parameters();

            for (Parameter p : ps) {
                idProperties.setProperty(p.name(), p.value());
            }
        } else {
            //nativegeneratordialect?
            igCls = "native";
        }
    } else if (gt == GenerationType.IDENTITY) {
        igCls = "identity";
    } else if (gt == GenerationType.SEQUENCE) {
        igCls = "sequence";

        javax.persistence.SequenceGenerator psg = (javax.persistence.SequenceGenerator) element
                .getAnnotation(javax.persistence.SequenceGenerator.class);
        if (psg == null) {
            Object sg = gf.getGlobalIdGenerator(generatorName);
            Assert.assertNotNull(sg,
                    "@javax.persistence.SequenceGenerator not found for sequenced @Id. domain class:"
                            + domainClas.getName());

            if (sg instanceof SequenceGenerator) {
                psg = (SequenceGenerator) sg;
            } else {
                throw new IllegalParameterException("The Id Generator [" + generatorName
                        + "] should be of type @javax.persistence.SequenceGenerator. domain class:"
                        + domainClas.getName());
            }
        }

        idProperties.setProperty(SequenceIdGenerator.PARAM_SEQUENCE, psg.sequenceName());

        idProperties.setProperty("catalog", psg.catalog());
        idProperties.setProperty("allocationSize", String.valueOf(psg.allocationSize()));
        idProperties.setProperty("initialValue", String.valueOf(psg.initialValue()));

        //we need db_group param, but the JPA won't give us.
    } else if (gt == GenerationType.TABLE) {
        igCls = "hilo.multi";

        TableGenerator pst = (TableGenerator) element.getAnnotation(TableGenerator.class);
        if (pst == null) {
            Object sg = gf.getGlobalIdGenerator(generatorName);
            Assert.assertNotNull(sg,
                    "@javax.persistence.TableGenerator not found for hilo.multi @Id. domain class:"
                            + domainClas.getName());

            if (sg instanceof TableGenerator) {
                pst = (TableGenerator) sg;
            } else {
                throw new IllegalParameterException("The Id Generator [" + generatorName
                        + "] should be of type @javax.persistence.TableGenerator. domain class:"
                        + domainClas.getName());
            }
        }

        idProperties.setProperty("catalog", pst.catalog());
        idProperties.setProperty("schema", pst.schema());
        idProperties.setProperty(TableMultiIdGenerator.TABLE, pst.table());
        idProperties.setProperty(TableMultiIdGenerator.PK_COLUMN_NAME, pst.pkColumnName());
        idProperties.setProperty(TableMultiIdGenerator.PK_COLUMN_VALUE, pst.pkColumnValue());
        idProperties.setProperty(TableMultiIdGenerator.COLUMN, pst.valueColumnName());
        idProperties.setProperty(TableMultiIdGenerator.MAX_LO, String.valueOf(pst.allocationSize()));
        //we need db_group param, but the JPA won't give us.

        idProperties.setProperty("initialValue", String.valueOf(pst.initialValue()));
    } else {
        throw new GuzzException("unknown @javax.persistence.GenerationType:" + gt);
    }

    if ("native".equals(igCls)) {
        igCls = dbGroup.getDialect().getNativeIDGenerator();
    }

    String realClassName = (String) IdentifierGeneratorFactory.getGeneratorClass(igCls);
    Assert.assertNotNull(realClassName, "unknown Id generator:" + igCls);

    IdentifierGenerator ig = (IdentifierGenerator) BeanCreator.newBeanInstance(realClassName);

    if (ig instanceof Configurable) {
        ((Configurable) ig).configure(dbGroup.getDialect(), map, idProperties);
    }

    //register callback for GuzzContext's full starting.
    if (ig instanceof GuzzContextAware) {
        gf.registerContextStartedAware((GuzzContextAware) ig);
    }

    st.setIdentifierGenerator(ig);
}

From source file:com.newline.view.company.entity.NlCompanyEntity.java

/**
 * : ?java.lang.Integer/*from  w w w . j  av  a 2 s . c om*/
 * 
 * @return: java.lang.Integer ??ID
 */
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID", nullable = false, length = 10)
public java.lang.Integer getId() {
    return this.id;
}

From source file:com.mmnaseri.dragonfly.metadata.impl.AnnotationTableMetadataResolver.java

private ValueGenerationType determineValueGenerationType(Method method) {
    if (method.isAnnotationPresent(GeneratedValue.class)) {
        final GeneratedValue generatedValue = method.getAnnotation(GeneratedValue.class);
        return generatedValue.strategy().equals(GenerationType.AUTO) ? dialect.getDefaultGenerationType()
                : (generatedValue.strategy().equals(GenerationType.IDENTITY) ? ValueGenerationType.IDENTITY
                        : (generatedValue.strategy().equals(GenerationType.SEQUENCE)
                                ? ValueGenerationType.SEQUENCE
                                : (generatedValue.strategy().equals(GenerationType.TABLE)
                                        ? ValueGenerationType.TABLE
                                        : null)));
    } else {/*w  w  w  . j av  a  2  s . co  m*/
        return null;
    }
}

From source file:org.apache.openjpa.persistence.XMLPersistenceMetaDataParser.java

/**
 * Parse generated-value.//from  w w  w .j  ava  2  s  .  c  o  m
 */
protected boolean startGeneratedValue(Attributes attrs) throws SAXException {
    if (!isMappingOverrideMode())
        return false;

    String strategy = attrs.getValue("strategy");
    String generator = attrs.getValue("generator");
    GenerationType type = StringUtils.isEmpty(strategy) ? GenerationType.AUTO
            : GenerationType.valueOf(strategy);

    FieldMetaData fmd = (FieldMetaData) currentElement();
    AnnotationPersistenceMetaDataParser.parseGeneratedValue(fmd, type, generator);
    return true;
}