From 6aa734e29ca05ad1f9144c41719383c2913f71bc Mon Sep 17 00:00:00 2001 From: Konstantin Krivopustov Date: Tue, 14 Jul 2015 07:15:30 +0000 Subject: [PATCH] EclipseLink ORM (fixed CCPayments tests, added @EmbeddedParameters, standard views include system props). #PL-5412 --- .../EclipseLinkSessionEventListener.java | 10 ++++++ .../entity/annotation/EmbeddedParameters.java | 31 +++++++++++++++++++ .../cuba/core/sys/AbstractViewRepository.java | 2 +- 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 modules/global/src/com/haulmont/cuba/core/entity/annotation/EmbeddedParameters.java diff --git a/modules/core/src/com/haulmont/cuba/core/sys/persistence/EclipseLinkSessionEventListener.java b/modules/core/src/com/haulmont/cuba/core/sys/persistence/EclipseLinkSessionEventListener.java index f180f55480..9be5059dcb 100644 --- a/modules/core/src/com/haulmont/cuba/core/sys/persistence/EclipseLinkSessionEventListener.java +++ b/modules/core/src/com/haulmont/cuba/core/sys/persistence/EclipseLinkSessionEventListener.java @@ -9,12 +9,14 @@ import com.haulmont.chile.core.model.MetaClass; import com.haulmont.chile.core.model.MetaProperty; import com.haulmont.cuba.core.entity.BaseEntity; import com.haulmont.cuba.core.entity.SoftDelete; +import com.haulmont.cuba.core.entity.annotation.EmbeddedParameters; import com.haulmont.cuba.core.global.AppBeans; import com.haulmont.cuba.core.global.Metadata; import com.haulmont.cuba.core.sys.UuidConverter; import org.eclipse.persistence.descriptors.ClassDescriptor; import org.eclipse.persistence.descriptors.DescriptorEventListener; import org.eclipse.persistence.internal.helper.DatabaseField; +import org.eclipse.persistence.mappings.AggregateObjectMapping; import org.eclipse.persistence.mappings.DatabaseMapping; import org.eclipse.persistence.mappings.DirectToFieldMapping; import org.eclipse.persistence.mappings.OneToOneMapping; @@ -58,6 +60,7 @@ public class EclipseLinkSessionEventListener extends SessionEventAdapter { List mappings = desc.getMappings(); for (DatabaseMapping mapping : mappings) { + // support UUID String attributeName = mapping.getAttributeName(); MetaProperty metaProperty = metaClass.getPropertyNN(attributeName); if (metaProperty.getRange().isDatatype()) { @@ -74,6 +77,13 @@ public class EclipseLinkSessionEventListener extends SessionEventAdapter { } } } + // embedded attributes + if (mapping instanceof AggregateObjectMapping) { + EmbeddedParameters embeddedParameters = + metaProperty.getAnnotatedElement().getAnnotation(EmbeddedParameters.class); + if (embeddedParameters != null && !embeddedParameters.nullAllowed()) + ((AggregateObjectMapping) mapping).setIsNullAllowed(false); + } } } } diff --git a/modules/global/src/com/haulmont/cuba/core/entity/annotation/EmbeddedParameters.java b/modules/global/src/com/haulmont/cuba/core/entity/annotation/EmbeddedParameters.java new file mode 100644 index 0000000000..2de2636df6 --- /dev/null +++ b/modules/global/src/com/haulmont/cuba/core/entity/annotation/EmbeddedParameters.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2008-2015 Haulmont. All rights reserved. + * Use is subject to license terms, see http://www.cuba-platform.com/license for details. + */ + +package com.haulmont.cuba.core.entity.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Defines additional parameters for Embedded attributes. + * + * @author Konstantin Krivopustov + * @version $Id$ + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.FIELD}) +public @interface EmbeddedParameters { + + /** + * If false, the embedded entity can not be null. This means you can always provide an instance when persisting + * the entity, even if all its attributes are null. + *

+ * By default, the embedded entity can be null. In this case ORM does not create the instance on loading + * if all attributes are null. + */ + boolean nullAllowed() default true; +} diff --git a/modules/global/src/com/haulmont/cuba/core/sys/AbstractViewRepository.java b/modules/global/src/com/haulmont/cuba/core/sys/AbstractViewRepository.java index 2bbaf5e1d3..31905149aa 100644 --- a/modules/global/src/com/haulmont/cuba/core/sys/AbstractViewRepository.java +++ b/modules/global/src/com/haulmont/cuba/core/sys/AbstractViewRepository.java @@ -268,7 +268,7 @@ public class AbstractViewRepository implements ViewRepository { name, metaClass.getName())); } - View view = new View(javaClass, name, false); + View view = new View(javaClass, name, true); // standard views include system attributes since v6 to avoid problems with assigning them automatically on saving if (View.LOCAL.equals(name)) { for (MetaProperty property : metaClass.getProperties()) { if (!property.getRange().isClass()