Example usage for javax.persistence InheritanceType SINGLE_TABLE

List of usage examples for javax.persistence InheritanceType SINGLE_TABLE

Introduction

In this page you can find the example usage for javax.persistence InheritanceType SINGLE_TABLE.

Prototype

InheritanceType SINGLE_TABLE

To view the source code for javax.persistence InheritanceType SINGLE_TABLE.

Click Source Link

Document

A single table per class hierarchy.

Usage

From source file:com.jaxio.celerio.factory.InheritanceFactory.java

private void putEntityByTableNameForEntityWithInheritance() {
    // Attention, for SINGLE_TABLE inheritance strategy, we only put the root entity.

    for (EntityConfig entityConfig : config.getCelerio().getEntityConfigs()) {
        Entity entity = config.getProject().getEntityByName(entityConfig.getEntityName());

        if (entity.hasInheritance() && !config.getProject().hasEntityByTableName(entity.getTableName())) {
            InheritanceType inheritanceType = entity.getInheritance().getStrategy();

            if (inheritanceType == InheritanceType.SINGLE_TABLE) {
                if (entity.isRoot()) {
                    config.getProject().putEntityByTableName(entity);
                }/*from  www.  j a  v a 2 s .c o  m*/
            } else if (inheritanceType == InheritanceType.JOINED
                    || inheritanceType == InheritanceType.TABLE_PER_CLASS) {
                config.getProject().putEntityByTableName(entity);
            } else {
                log.warning("Invalid case, there should be an inheritance type");
            }
        }
    }
}

From source file:org.ibankapp.base.persistence.validation.test.UniqueValidatorTest.java

@Test
@Transactional//  w w  w  .ja v a2  s.co  m
public void testUniqueAndNum() {
    thrown.expect(BaseException.class);
    thrown.expectMessage("?,ENUM??");

    TestModelWithEumAndUnique model = new TestModelWithEumAndUnique();
    model.setStatus(InheritanceType.SINGLE_TABLE);
    model.setId("aaa");
    repository.persist(model);

    model = new TestModelWithEumAndUnique();
    model.setId("bbb");
    model.setStatus(InheritanceType.SINGLE_TABLE);
    repository.persist(model);
}

From source file:com.jaxio.celerio.factory.EntityConfigFactory.java

private void resolveMissingInheritanceStrategyOnEntityConfigs(
        Map<String, EntityConfig> entityConfigsByEntityName) {
    for (EntityConfig entityConfig : entityConfigsByEntityName.values()) {
        if (!entityConfig.hasInheritance()) {
            continue;
        }//from  w  w w  .  j  ava  2  s.  com

        EntityConfig current = entityConfig;
        while (current.hasParentEntityName()) {
            current = entityConfigsByEntityName.get(current.getParentEntityName().toUpperCase());
            Assert.notNull(current, "The parent entity " + current.getParentEntityName()
                    + " could not be found in the configuration.");
        }
        // root may use default...
        if (!current.getInheritance().hasStrategy()) {
            // default...
            current.getInheritance().setStrategy(InheritanceType.SINGLE_TABLE);
        }

        if (entityConfig.getInheritance().hasStrategy()) {
            Assert.isTrue(entityConfig.getInheritance().getStrategy() == current.getInheritance().getStrategy(),
                    "The entityConfig " + entityConfig.getEntityName()
                            + " must not declare an inheritance strategy that is different from the strategy declared in the root entity "
                            + current.getEntityName());
        }

        // for internal convenient purposes we propagate it
        entityConfig.getInheritance().setStrategy(current.getInheritance().getStrategy());
    }
}

From source file:org.batoo.jpa.core.impl.model.EntityTypeImpl.java

private synchronized void setInherited() {
    if (this.inheritanceType == null) {
        this.inheritanceType = InheritanceType.SINGLE_TABLE;

        if (this.discriminatorColumn == null) {
            this.discriminatorColumn = new DiscriminatorColumn(this.getMetamodel().getJdbcAdaptor(),
                    this.primaryTable, this.metadata.getDiscriminatorColumn());
        }/*from   ww  w . ja  v  a  2  s  .  c  o  m*/
    }
}

From source file:org.broadleafcommerce.common.extensibility.jpa.convert.inheritance.SingleTableInheritanceClassTransformer.java

@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
        ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
    // Lambdas and anonymous methods in Java 8 do not have a class name defined and so no transformation should be done
    if (className == null) {
        return null;
    }/* www.  jav  a  2  s .  c o  m*/

    if (infos.isEmpty()) {
        return null;
    }
    String convertedClassName = className.replace('/', '.');
    SingleTableInheritanceInfo key = new SingleTableInheritanceInfo();
    key.setClassName(convertedClassName);
    int pos = infos.indexOf(key);
    if (pos >= 0) {
        try {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Converting " + convertedClassName + " to a SingleTable inheritance strategy.");
            }
            SingleTableInheritanceInfo myInfo = infos.get(pos);
            ClassFile classFile = new ClassFile(new DataInputStream(new ByteArrayInputStream(classfileBuffer)));
            ConstPool constantPool = classFile.getConstPool();
            AnnotationsAttribute annotationsAttribute = new AnnotationsAttribute(constantPool,
                    AnnotationsAttribute.visibleTag);
            List<?> attributes = classFile.getAttributes();
            Iterator<?> itr = attributes.iterator();
            while (itr.hasNext()) {
                Object object = itr.next();
                if (AnnotationsAttribute.class.isAssignableFrom(object.getClass())) {
                    AnnotationsAttribute attr = (AnnotationsAttribute) object;
                    Annotation[] items = attr.getAnnotations();
                    for (Annotation annotation : items) {
                        String typeName = annotation.getTypeName();
                        if (!typeName.equals(Inheritance.class.getName())) {
                            annotationsAttribute.addAnnotation(annotation);
                        }
                    }
                    itr.remove();
                }
            }
            Annotation inheritance = new Annotation(Inheritance.class.getName(), constantPool);
            ClassPool pool = ClassPool.getDefault();
            pool.importPackage("javax.persistence");
            pool.importPackage("java.lang");
            EnumMemberValue strategy = (EnumMemberValue) Annotation.createMemberValue(constantPool,
                    pool.makeClass("InheritanceType"));
            strategy.setType(InheritanceType.class.getName());
            strategy.setValue(InheritanceType.SINGLE_TABLE.name());
            inheritance.addMemberValue("strategy", strategy);
            annotationsAttribute.addAnnotation(inheritance);
            if (myInfo.getDiscriminatorName() != null) {
                Annotation discriminator = new Annotation(DiscriminatorColumn.class.getName(), constantPool);
                StringMemberValue name = new StringMemberValue(constantPool);
                name.setValue(myInfo.getDiscriminatorName());
                discriminator.addMemberValue("name", name);
                EnumMemberValue discriminatorType = (EnumMemberValue) Annotation.createMemberValue(constantPool,
                        pool.makeClass("DiscriminatorType"));
                discriminatorType.setType(DiscriminatorType.class.getName());
                discriminatorType.setValue(myInfo.getDiscriminatorType().name());
                discriminator.addMemberValue("discriminatorType", discriminatorType);
                IntegerMemberValue length = new IntegerMemberValue(constantPool);
                length.setValue(myInfo.getDiscriminatorLength());
                discriminator.addMemberValue("length", length);

                annotationsAttribute.addAnnotation(discriminator);
            }
            classFile.addAttribute(annotationsAttribute);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            DataOutputStream os = new DataOutputStream(bos);
            classFile.write(os);
            os.close();

            return bos.toByteArray();
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new IllegalClassFormatException("Unable to convert " + convertedClassName
                    + " to a SingleTable inheritance strategy: " + ex.getMessage());
        }
    } else {
        return null;
    }
}