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

tech.ydb.yoj.repository.db.Entity Maven / Gradle / Ivy

Go to download

Core YOJ (YDB ORM for Java) abstractions and APIs for domain entities, repositories, transactions etc.

The newest version!
package tech.ydb.yoj.repository.db;

import com.google.common.reflect.TypeToken;
import lombok.NonNull;
import tech.ydb.yoj.DeprecationWarnings;

import javax.annotation.CheckForNull;
import java.util.List;
import java.util.function.Supplier;

public interface Entity> extends Table.ViewId {
    @Override
    Id getId();

    @SuppressWarnings("unchecked")
    default E postLoad() {
        return (E) this;
    }

    @SuppressWarnings("unchecked")
    default E preSave() {
        return (E) this;
    }

    default List> createProjections() {
        return List.of();
    }

    interface Id> {
        /**
         * @deprecated This method will be removed in YOJ 3.0.0. Use {@link Table#find(Entity.Id)} instead.
         */
        @CheckForNull
        @Deprecated(forRemoval = true)
        default E resolve() {
            DeprecationWarnings.warnOnce(
                    "Entity.Id.resolve()",
                    "You are using Entity.Id.resolve() which will be removed in YOJ 3.0.0. Please use Table.find(ID)"
            );
            return Tx.Current.get().getRepositoryTransaction().table(getType()).find(this);
        }

        /**
         * @deprecated This method will be removed in YOJ 3.0.0. Use {@link Table#find(Entity.Id, Supplier)} instead.
         */
        @NonNull
        @Deprecated(forRemoval = true)
        default  E resolve(
                Supplier throwIfAbsent
        ) throws EXCEPTION {
            DeprecationWarnings.warnOnce(
                    "Entity.Id.resolve()",
                    "You are using Entity.Id.resolve(Supplier) which will be removed in YOJ 3.0.0. Please use Table.find(ID, Supplier)"
            );
            return Tx.Current.get().getRepositoryTransaction().table(getType()).find(this, throwIfAbsent);
        }

        @SuppressWarnings("unchecked")
        default Class getType() {
            return (Class) new TypeToken(getClass()) {
            }.getRawType();
        }

        default boolean isPartial() {
            var schema = EntitySchema.of(getType()).getIdSchema();
            var columns = schema.flattenFields();
            var nonNullFields = schema.flatten(this);
            return columns.size() > nonNullFields.size();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy