tech.ydb.yoj.repository.db.Entity Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of yoj-repository Show documentation
Show all versions of yoj-repository Show documentation
Core YOJ (YDB ORM for Java) abstractions and APIs for domain entities, repositories, transactions etc.
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 extends EXCEPTION> 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();
}
}
}