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:uk.nhs.cfh.dsp.srth.demographics.person.impl.AbstractPerson.java

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public long getId() {
    return id;
}

From source file:velo.entity.Resource.java

/**
 * Get the ID of the entity/*ww w . ja  v a2  s .c  om*/
 * 
 * @return The ID of the entity to get
 */
@Id
//@GeneratedValue
@GeneratedValue(strategy = GenerationType.AUTO, generator = "ResourceIdSeq")
@Column(name = "RESOURCE_ID")
public Long getResourceId() {
    return resourceId;
}

From source file:org.grails.datastore.gorm.jpa.GormToJpaTransform.java

public static void transformEntity(SourceUnit source, ClassNode classNode) {

    // add the JPA @Entity annotation
    classNode.addAnnotation(ANNOTATION_ENTITY);
    final AnnotationNode entityListenersAnnotation = new AnnotationNode(new ClassNode(EntityListeners.class));
    entityListenersAnnotation.addMember("value",
            new ClassExpression(new ClassNode(EntityInterceptorInvokingEntityListener.class)));
    classNode.addAnnotation(entityListenersAnnotation);

    PropertyNode mappingNode = classNode.getProperty(GrailsDomainClassProperty.MAPPING);
    Map<String, Map<String, ?>> propertyMappings = new HashMap<String, Map<String, ?>>();
    if (mappingNode != null && mappingNode.isStatic()) {
        populateConfigurationMapFromClosureExpression(classNode, mappingNode, propertyMappings);
    }/* w  w  w  .  j  a v  a 2s .  com*/

    // annotate the id property with @Id
    String idPropertyName = GrailsDomainClassProperty.IDENTITY;
    String generationType = GenerationType.AUTO.toString();

    final PropertyNode errorsProperty = classNode.getProperty("errors");
    if (errorsProperty == null) {
        if (ClassUtils.isPresent("org.codehaus.groovy.grails.compiler.injection.ASTValidationErrorsHelper",
                Thread.currentThread().getContextClassLoader())) {
            addErrorsProperty(classNode);
        }
    }

    if (propertyMappings.containsKey(GrailsDomainClassProperty.IDENTITY)) {
        final Map<String, ?> idConfig = propertyMappings.get(GrailsDomainClassProperty.IDENTITY);
        if (idConfig.containsKey("name")) {
            idPropertyName = idConfig.get("name").toString();
        }
        if (idConfig.containsKey("generator")) {
            String generatorName = idConfig.get("generator").toString();
            if ("assigned".equals(generatorName)) {
                generationType = null;
            } else if ("sequence".equals(generatorName)) {
                generationType = GenerationType.SEQUENCE.toString();
            } else if ("identity".equals(generatorName)) {
                generationType = GenerationType.IDENTITY.toString();
            }
        }
    }

    PropertyNode idProperty = classNode.getProperty(idPropertyName);
    if (idProperty == null) {
        new DefaultGrailsDomainClassInjector().performInjectionOnAnnotatedEntity(classNode);
        idProperty = classNode.getProperty(GrailsDomainClassProperty.IDENTITY);
    }

    if (!idPropertyName.equals(GrailsDomainClassProperty.IDENTITY)) {
        PropertyNode toDiscard = classNode.getProperty(GrailsDomainClassProperty.IDENTITY);
        if (toDiscard != null && toDiscard.getType().equals("java.lang.Long")) {
            classNode.getProperties().remove(toDiscard);
        }
    }

    if (idProperty != null) {
        final FieldNode idField = idProperty.getField();

        idField.addAnnotation(ANNOTATION_ID);
        if (generationType != null) {
            final AnnotationNode generatedValueAnnotation = new AnnotationNode(
                    new ClassNode(GeneratedValue.class));
            generatedValueAnnotation.addMember("strategy", new PropertyExpression(
                    new ClassExpression(new ClassNode(GenerationType.class)), generationType));
            idField.addAnnotation(generatedValueAnnotation);
        }
    }

    // annotate the version property with @Version
    PropertyNode versionProperty = classNode.getProperty(GrailsDomainClassProperty.VERSION);
    if (versionProperty != null) {
        if (propertyMappings.containsKey(GrailsDomainClassProperty.VERSION)) {
            final Map<String, ?> versionSettings = propertyMappings.get(GrailsDomainClassProperty.VERSION);
            final Object enabledObject = versionSettings.get("enabled");
            if (enabledObject instanceof Boolean) {
                if (((Boolean) enabledObject).booleanValue()) {
                    versionProperty.addAnnotation(ANNOTATION_VERSION);
                }
            }
        } else {
            versionProperty.addAnnotation(ANNOTATION_VERSION);
        }
    }

    final List<MethodNode> methods = classNode.getMethods();
    for (MethodNode methodNode : methods) {
        if (methodNode.isStatic() || !methodNode.isPublic() || methodNode.isAbstract()) {
            continue;
        }

        final AnnotationNode annotationNode = gormEventMethodToJpaAnnotation.get(methodNode.getName());
        if (annotationNode == null) {
            continue;
        }

        //methodNode.setReturnType(new ClassNode(void.class));
        methodNode.addAnnotation(annotationNode);
    }

    Map<String, ClassNode> hasManyMap = lookupStringToClassNodeMap(classNode,
            GrailsDomainClassProperty.HAS_MANY);
    Map<String, ClassNode> hasOneMap = lookupStringToClassNodeMap(classNode, GrailsDomainClassProperty.HAS_ONE);
    Map<String, ClassNode> belongsToMap = lookupStringToClassNodeMap(classNode,
            GrailsDomainClassProperty.BELONGS_TO);
    Map<String, String> mappedByMap = lookupStringToStringMap(classNode, GrailsDomainClassProperty.MAPPED_BY);

    final List<PropertyNode> properties = classNode.getProperties();
    for (PropertyNode propertyNode : properties) {
        if (!propertyNode.isPublic() || propertyNode.isStatic()) {
            continue;
        }

        if (propertyNode == idProperty || propertyNode == versionProperty) {
            continue;
        }

        final String typeName = propertyNode.getType().getName();

        if (typeName.equals("java.util.Date") || typeName.equals("java.util.Calendar")) {
            AnnotationNode temporalAnnotation = new AnnotationNode(new ClassNode(Temporal.class));
            temporalAnnotation.addMember("value",
                    new PropertyExpression(new ClassExpression(new ClassNode(TemporalType.class)), "DATE"));
            propertyNode.getField().addAnnotation(temporalAnnotation);
        } else if (MappingFactory.isSimpleType(typeName)) {
            propertyNode.getField().addAnnotation(ANNOTATION_BASIC);
        } else {
            final String propertyName = propertyNode.getName();
            if (!belongsToMap.containsKey(propertyName) && !hasOneMap.containsKey(propertyName)
                    && !hasManyMap.containsKey(propertyName)) {
                handleToOne(classNode, belongsToMap, propertyName);
            }
        }
    }

    final PropertyNode transientsProp = classNode.getProperty(GrailsDomainClassProperty.TRANSIENT);
    List<String> propertyNameList = new ArrayList<String>();
    populateConstantList(propertyNameList, transientsProp);
    annotateAllProperties(classNode, propertyNameList, Transient.class);

    propertyNameList.clear();
    final PropertyNode embeddedProp = classNode.getProperty(GrailsDomainClassProperty.EMBEDDED);
    populateConstantList(propertyNameList, embeddedProp);
    annotateAllProperties(classNode, propertyNameList, Embedded.class);

    if (embeddedProp != null) {
        for (String propertyName : propertyNameList) {
            final PropertyNode property = classNode.getProperty(propertyName);
            if (property == null) {
                continue;
            }

            ClassNode embeddedType = property.getField().getType();
            annotateIfNecessary(embeddedType, Embeddable.class);
        }
    }

    if (!belongsToMap.isEmpty()) {
        for (String propertyName : belongsToMap.keySet()) {
            handleToOne(classNode, belongsToMap, propertyName);
        }
    }

    if (!hasOneMap.isEmpty()) {
        for (String propertyName : hasOneMap.keySet()) {
            final AnnotationNode oneToOneAnnotation = new AnnotationNode(new ClassNode(OneToOne.class));
            oneToOneAnnotation.addMember("optional", ConstantExpression.FALSE);
            oneToOneAnnotation.addMember("cascade", EXPR_CASCADE_ALL);
            annotateProperty(classNode, propertyName, oneToOneAnnotation);
        }
    }

    if (!hasManyMap.isEmpty()) {
        for (String propertyName : hasManyMap.keySet()) {
            ClassNode associatedClass = hasManyMap.get(propertyName);
            final Map<String, ClassNode> inverseBelongsToMap = lookupStringToClassNodeMap(associatedClass,
                    GrailsDomainClassProperty.BELONGS_TO);
            final Map<String, ClassNode> inverseHasManyMap = lookupStringToClassNodeMap(associatedClass,
                    GrailsDomainClassProperty.HAS_MANY);

            final AnnotationNode oneToManyAnnotation = new AnnotationNode(new ClassNode(OneToMany.class));
            oneToManyAnnotation.addMember("targetEntity", new ClassExpression(associatedClass));

            if (mappedByMap.containsKey(propertyName)) {
                oneToManyAnnotation.addMember("mappedBy",
                        new ConstantExpression(mappedByMap.get(propertyName)));
                oneToManyAnnotation.addMember("cascade", EXPR_CASCADE_PERSIST);
                annotateProperty(classNode, propertyName, oneToManyAnnotation);
            } else {
                if (inverseHasManyMap.containsValue(classNode)) {
                    // many-to-many association
                    List<ClassNode> belongsToList = getBelongsToList(classNode);

                    final AnnotationNode manyToManyAnnotation = new AnnotationNode(
                            new ClassNode(ManyToMany.class));
                    manyToManyAnnotation.addMember("targetEntity", new ClassExpression(associatedClass));
                    if (belongsToList.contains(associatedClass)) {
                        for (String inversePropertyName : inverseHasManyMap.keySet()) {
                            if (classNode.equals(inverseHasManyMap.get(inversePropertyName))) {
                                manyToManyAnnotation.addMember("mappedBy",
                                        new ConstantExpression(inversePropertyName));
                            }
                        }
                    } else {
                        manyToManyAnnotation.addMember("cascade", EXPR_CASCADE_ALL);
                    }
                    annotateProperty(classNode, propertyName, manyToManyAnnotation);
                }
                // Try work out the other side of the association
                else if (inverseBelongsToMap.containsValue(classNode)) {
                    for (String inversePropertyName : inverseBelongsToMap.keySet()) {
                        if (classNode.equals(inverseBelongsToMap.get(inversePropertyName))) {
                            oneToManyAnnotation.addMember("mappedBy",
                                    new ConstantExpression(inversePropertyName));
                            oneToManyAnnotation.addMember("cascade", EXPR_CASCADE_ALL);
                        }
                    }
                    annotateProperty(classNode, propertyName, oneToManyAnnotation);
                } else {
                    PropertyNode inverseClosestMatch = findClosestInverstTypeMatch(classNode, associatedClass);
                    if (inverseClosestMatch != null) {
                        oneToManyAnnotation.addMember("mappedBy",
                                new ConstantExpression(inverseClosestMatch.getName()));
                    }
                    // unidrectional one-to-many
                    oneToManyAnnotation.addMember("cascade", EXPR_CASCADE_ALL);
                    annotateProperty(classNode, propertyName, oneToManyAnnotation);
                }
            }
        }
    }
}

From source file:com.hive.enterprisemanage.entity.EEnterpriseinfo.java

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@GenericGenerator(name = "idGenerator", strategy = "native")
@Column(name = "NENTERPRISEID", unique = true, nullable = true)
public Long getNenterpriseid() {
    return this.nenterpriseid;
}

From source file:gov.nih.nci.firebird.data.user.FirebirdUser.java

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Searchable
@Override
public Long getId() {
    return id;
}

From source file:com.fiveamsolutions.nci.commons.audit.AuditLogRecord.java

/**
 * @return database identifier
 */
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
    return this.id;
}

From source file:com.fiveamsolutions.nci.commons.data.security.AbstractUser.java

/**
 * {@inheritDoc}
 */
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
    return id;
}

From source file:org.apache.ranger.audit.entity.AuthzAuditEventDbObj.java

@Id
@SequenceGenerator(name = "XA_ACCESS_AUDIT_SEQ", sequenceName = "XA_ACCESS_AUDIT_SEQ", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.AUTO, generator = "XA_ACCESS_AUDIT_SEQ")
@Column(name = "id", unique = true, nullable = false)
public long getAuditId() {
    return this.auditId;
}

From source file:org.tonguetied.usermanagement.User.java

/**
 * @return the set of authorized permissions granted to a User
 *//*from  w  ww  . ja  v  a 2s .co m*/
@CollectionOfElements(fetch = FetchType.LAZY)
@Sort(type = SortType.NATURAL)
@JoinTable(name = TABLE_AUTHORITIES, joinColumns = @JoinColumn(name = "user_id"))
@Cascade(CascadeType.ALL)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@GeneratedValue(strategy = GenerationType.AUTO, generator = "authorities_generator")
@SequenceGenerator(name = "authorities_generator", sequenceName = "authorities_user_id_seq")
@ForeignKey(name = FK_AUTHORITIES)
public SortedSet<UserRight> getUserRights() {
    return userRights;
}