All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.openjpa.persistence.meta.Types Maven / Gradle / Ivy

There is a newer version: 4.0.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.openjpa.persistence.meta;

import javax.persistence.metamodel.EmbeddableType;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.IdentifiableType;
import javax.persistence.metamodel.MappedSuperclassType;
import javax.persistence.metamodel.Type;

import org.apache.openjpa.lib.util.Localizer;
import org.apache.openjpa.meta.ClassMetaData;

/**
 * Persistent Type according to JPA 2.0.
 * 
* JPA 2.0 defines a type system for persistent objects to decorate a core Java type system * with persistence-relevant properties such as persistent identity, independently persistence * capable etc. *
* Implemented as a thin adapter to OpenJPA metadata system. Mostly immutable. * * @author Pinaki Poddar * * @since 2.0.0 * */ public class Types { protected static Localizer _loc = Localizer.forPackage(Types.class); /** * Mirrors a Java class. * * @param Java class */ static abstract class BaseType implements Type { public final Class cls; protected BaseType(Class cls) { this.cls = cls; } public final Class getJavaType() { return cls; } public String toString() { return cls.getName(); } } /** * Basic non-relational types of a persistent attribute such as long or * java.util.Date. * * @author Pinaki Poddar * * @param represented Java type. */ public static class Basic extends BaseType implements Type { public Basic(Class cls) { super(cls); } public PersistenceType getPersistenceType() { return PersistenceType.BASIC; } } /** * Represents an abstract persistent type that has a persistent identity. * * @author Pinaki Poddar * * @param */ public static abstract class Identifiable extends AbstractManagedType implements IdentifiableType { public Identifiable(ClassMetaData meta, MetamodelImpl model) { super(meta, model); } /** * Whether or not the identifiable type has a version attribute. * @return boolean indicating whether or not the identifiable * type has a version attribute */ public boolean hasVersionAttribute() { return meta.getVersionField() != null; } /** * Return the identifiable type that corresponds to the most * specific mapped superclass or entity extended by the entity * or mapped superclass. * @return super type of identifiable type or null if no such super type */ public IdentifiableType getSupertype() { ClassMetaData superMeta = meta.getPCSuperclassMetaData(); if (superMeta == null) return null; return (IdentifiableType) model.managedType(superMeta.getDescribedType()); } public boolean hasIdAttribute() { return meta.getIdentityType() == ClassMetaData.ID_APPLICATION; } /** * Whether or not the identifiable type uses an attribute to represents its persistent identity. * Returns true for a simple or embedded identifier. * Returns false for an classes that use separate identifier class for its persistent identity. * * @return boolean indicating whether or not the identifiable * type represents its persistent identity via a single identifier attribute. */ public boolean hasSingleIdAttribute() { return meta.getPrimaryKeyFields().length == 1; } /** * Return the type that represents the type of the id. * @return type of identifier */ public Type getIdType() { Class idType = hasSingleIdAttribute() ? meta.getPrimaryKeyFields()[0].getDeclaredType() : meta.getObjectIdType(); return model.getType(idType); } } /** * An embedded, not independently identifiable type. * * @author Pinaki Poddar * * @param the represented Java type. */ public static class Embeddable extends AbstractManagedType implements EmbeddableType { public Embeddable(ClassMetaData meta, MetamodelImpl model) { super(meta, model); } public PersistenceType getPersistenceType() { return PersistenceType.EMBEDDABLE; } } /** * A abstract, independently identifiable persistent type. * * @author Pinaki Poddar * * @param the represented Java type. */ public static class MappedSuper extends Identifiable implements MappedSuperclassType { public MappedSuper(ClassMetaData meta, MetamodelImpl model) { super(meta, model); } public PersistenceType getPersistenceType() { return PersistenceType.MAPPED_SUPERCLASS; } } /** * An entity type that is independently identifiable. * * @author Pinaki Poddar * * @param the represented Java type. */ public static class Entity extends Identifiable implements EntityType { public Entity(ClassMetaData meta, MetamodelImpl model) { super(meta, model); } public PersistenceType getPersistenceType() { return PersistenceType.ENTITY; } public String getName() { return meta.getTypeAlias(); } /** * Return the bindable type of the represented object. * @return bindable type */ public BindableType getBindableType() { return BindableType.ENTITY_TYPE; } /** * Return the Java type of the represented object. * If the bindable type of the object is PLURAL_ATTRIBUTE, * the Java element type is returned. If the bindable type is * SINGULAR_ATTRIBUTE or ENTITY_TYPE, the Java type of the * represented entity or attribute is returned. * @return Java type */ public Class getBindableJavaType() { return getJavaType(); } } /** * A pseudo managed type used to represent keys of a java.util.Map as a * pseudo attribute. **/ public static class PseudoEntity extends AbstractManagedType { protected PseudoEntity(Class cls, MetamodelImpl model) { super(cls, model); } public javax.persistence.metamodel.Type.PersistenceType getPersistenceType() { return PersistenceType.ENTITY; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy