Search in sources :

Example 1 with EmbeddedId

use of jakarta.persistence.EmbeddedId in project hibernate-orm by hibernate.

the class AnnotationBinder method processId.

private static void processId(PropertyHolder propertyHolder, PropertyData inferredData, SimpleValue idValue, Map<String, IdentifierGeneratorDefinition> classGenerators, boolean isIdentifierMapper, MetadataBuildingContext buildingContext) {
    if (isIdentifierMapper) {
        throw new AnnotationException("@IdClass class should not have @Id nor @EmbeddedId properties: " + BinderHelper.getPath(propertyHolder, inferredData));
    }
    XClass entityXClass = inferredData.getClassOrElement();
    XProperty idXProperty = inferredData.getProperty();
    final Annotation generatorAnnotation = HCANNHelper.findContainingAnnotation(idXProperty, IdGeneratorType.class);
    if (generatorAnnotation != null) {
        idValue.setCustomIdGeneratorCreator(new CustomIdGeneratorCreator(generatorAnnotation, idXProperty));
    } else {
        // manage composite related metadata
        // guess if its a component and find id data access (property, field etc)
        final boolean isComponent = entityXClass.isAnnotationPresent(Embeddable.class) || idXProperty.isAnnotationPresent(EmbeddedId.class);
        GeneratedValue generatedValue = idXProperty.getAnnotation(GeneratedValue.class);
        String generatorType = generatedValue != null ? generatorType(generatedValue, buildingContext, entityXClass) : "assigned";
        String generatorName = generatedValue != null ? generatedValue.generator() : "";
        if (isComponent) {
            // a component must not have any generator
            generatorType = "assigned";
        }
        if (isGlobalGeneratorNameGlobal(buildingContext)) {
            buildGenerators(idXProperty, buildingContext);
            SecondPass secondPass = new IdGeneratorResolverSecondPass(idValue, idXProperty, generatorType, generatorName, buildingContext);
            buildingContext.getMetadataCollector().addSecondPass(secondPass);
        } else {
            // clone classGenerator and override with local values
            HashMap<String, IdentifierGeneratorDefinition> localGenerators = new HashMap<>(classGenerators);
            localGenerators.putAll(buildGenerators(idXProperty, buildingContext));
            makeIdGenerator(idValue, idXProperty, generatorType, generatorName, buildingContext, localGenerators);
        }
        if (LOG.isTraceEnabled()) {
            LOG.tracev("Bind {0} on {1}", (isComponent ? "@EmbeddedId" : "@Id"), inferredData.getPropertyName());
        }
    }
}
Also used : XProperty(org.hibernate.annotations.common.reflection.XProperty) HashMap(java.util.HashMap) EmbeddedId(jakarta.persistence.EmbeddedId) XClass(org.hibernate.annotations.common.reflection.XClass) AnnotatedColumn.buildColumnFromAnnotation(org.hibernate.cfg.AnnotatedColumn.buildColumnFromAnnotation) OverridesAnnotation(org.hibernate.annotations.DialectOverride.OverridesAnnotation) Annotation(java.lang.annotation.Annotation) AnnotatedColumn.buildFormulaFromAnnotation(org.hibernate.cfg.AnnotatedColumn.buildFormulaFromAnnotation) AnnotatedColumn.buildColumnFromNoAnnotation(org.hibernate.cfg.AnnotatedColumn.buildColumnFromNoAnnotation) Embeddable(jakarta.persistence.Embeddable) GeneratedValue(jakarta.persistence.GeneratedValue) NullableDiscriminatorColumnSecondPass(org.hibernate.cfg.internal.NullableDiscriminatorColumnSecondPass) IdentifierGeneratorDefinition(org.hibernate.boot.model.IdentifierGeneratorDefinition) AnnotationException(org.hibernate.AnnotationException)

Aggregations

Embeddable (jakarta.persistence.Embeddable)1 EmbeddedId (jakarta.persistence.EmbeddedId)1 GeneratedValue (jakarta.persistence.GeneratedValue)1 Annotation (java.lang.annotation.Annotation)1 HashMap (java.util.HashMap)1 AnnotationException (org.hibernate.AnnotationException)1 OverridesAnnotation (org.hibernate.annotations.DialectOverride.OverridesAnnotation)1 XClass (org.hibernate.annotations.common.reflection.XClass)1 XProperty (org.hibernate.annotations.common.reflection.XProperty)1 IdentifierGeneratorDefinition (org.hibernate.boot.model.IdentifierGeneratorDefinition)1 AnnotatedColumn.buildColumnFromAnnotation (org.hibernate.cfg.AnnotatedColumn.buildColumnFromAnnotation)1 AnnotatedColumn.buildColumnFromNoAnnotation (org.hibernate.cfg.AnnotatedColumn.buildColumnFromNoAnnotation)1 AnnotatedColumn.buildFormulaFromAnnotation (org.hibernate.cfg.AnnotatedColumn.buildFormulaFromAnnotation)1 NullableDiscriminatorColumnSecondPass (org.hibernate.cfg.internal.NullableDiscriminatorColumnSecondPass)1