org.rx.io.EntityDatabase Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxlib Show documentation
Show all versions of rxlib Show documentation
A set of utilities for Java
package org.rx.io;
import lombok.SneakyThrows;
import org.rx.bean.DataTable;
import org.rx.util.function.Action;
import org.rx.util.function.Func;
import java.io.Serializable;
import java.util.List;
public interface EntityDatabase extends AutoCloseable {
String DEFAULT_FILE_PATH = "./rx";
EntityDatabase DEFAULT = new EntityDatabaseImpl();
void save(T entity);
void save(T entity, boolean doInsert);
boolean deleteById(Class entityType, Serializable id);
long delete(EntityQueryLambda query);
long count(EntityQueryLambda query);
boolean exists(EntityQueryLambda query);
boolean existsById(Class entityType, Serializable id);
T findById(Class entityType, Serializable id);
T findOne(EntityQueryLambda query);
List findBy(EntityQueryLambda query);
void compact();
void dropMapping(Class entityType);
void createMapping(Class>... entityTypes);
String tableName(Class> entityType);
DataTable executeQuery(String sql, Class entityType);
int executeUpdate(String sql);
@SneakyThrows
default void transInvoke(int transactionIsolation, Action fn) {
boolean doCommit = false;
begin(transactionIsolation);
try {
fn.invoke();
doCommit = true;
} finally {
if (doCommit) {
commit();
} else {
rollback();
}
}
}
@SneakyThrows
default T transInvoke(int transactionIsolation, Func fn) {
boolean doCommit = false;
begin(transactionIsolation);
try {
T r = fn.invoke();
doCommit = true;
return r;
} finally {
if (doCommit) {
commit();
} else {
rollback();
}
}
}
boolean isInTransaction();
void begin();
void begin(int transactionIsolation);
void commit();
void rollback();
}