
sf.core.DBObject Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sorm Show documentation
Show all versions of sorm Show documentation
java jpa tool for spring
The newest version!
package sf.core;
import sf.database.meta.ColumnMapping;
import sf.database.meta.MetaHolder;
import sf.database.meta.TableMapping;
import sf.database.util.OrmUtils;
import sf.dsl.example.Example;
import java.io.Serializable;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
/**
* 抽象类,用于实现所有Entity默认的各种方法
* 为了避免jackson,fastjson,gson出现不该序列化的字段,需要避免get,set,is等方法出现.
*/
@SuppressWarnings("serial")
public abstract class DBObject implements IEntity {
private static final ConditionComparator cmp = new ConditionComparator();
protected transient Map updateValueMap;
protected transient boolean _recordUpdate = true;
protected transient Example query;
transient ILazyLoadContext lazyload;
private transient String _rowid;
/**
* 额外属性(查询时未转换为bean属性的额外数据库字段)
*/
private Map tail;
/**
* 使用查询
* @return
*/
public final Example useQuery() {
if (query == null) {
query = new Example(this.getClass());
}
return query;
}
public final boolean hasQuery() {
return query != null;
}
public final void clearQuery() {
query = null;
lazyload = null;
}
@Override
public final void startUpdate() {
_recordUpdate = true;
}
@Override
public final void stopUpdate() {
_recordUpdate = false;
}
@Override
public final boolean hasUsed(DBField field) {
if (updateValueMap == null) {
return false;
}
return updateValueMap.containsKey(field);
}
@Override
public final void clearUpdate() {
updateValueMap = null;
}
@Override
public final Map updateValueMap() {
if (updateValueMap == null) {
return Collections.emptyMap();
}
return updateValueMap;
// if (updateValueMap != null && !updateValueMap.isEmpty()) {
// return updateValueMap;
// }
// return OrmUtils.getDataObjectNotNullValue(this);//提取非空值
}
@Override
public final void prepareUpdate(DBField field, Object newValue) {
if (updateValueMap == null) {
updateValueMap = new TreeMap<>(cmp);
}
updateValueMap.put(field, newValue);
}
void markUpdateFlag(DBField field, Object newValue) {
if (updateValueMap == null) {
updateValueMap = new TreeMap<>(cmp);
}
updateValueMap.put(field, newValue);
}
@Override
public void newUpdate() {
updateValueMap = new TreeMap<>(cmp);
}
@Override
public final void applyUpdate() {
if (updateValueMap == null) return;
for (Entry entry : updateValueMap.entrySet()) {
Object newValue = entry.getValue();
if (newValue instanceof DBField) {
continue;
}
}
clearUpdate();
}
@Override
public final boolean needUpdate() {
return (updateValueMap != null) && this.updateValueMap.size() > 0;
}
@Override
public String rowid() {
return _rowid;
}
@Override
public void bindRowid(String rowid) {
this._rowid = rowid;
}
/*
* 供子类hashCode()方法调用,判断内嵌的hashCode方法是否可用
*/
protected final int calculateHashCode() {
int result = updateValueMap != null ? updateValueMap.hashCode() : 0;
result = 31 * result + (_recordUpdate ? 1 : 0);
result = 31 * result + (_rowid != null ? _rowid.hashCode() : 0);
result = 31 * result + (tail != null ? tail.hashCode() : 0);
return result;
}
protected final void beforeSet(String fieldname) {
if (lazyload == null) return;
lazyload.markProcessed(fieldname);
}
/*
* 处理延迟加载的字段
*/
protected final void beforeGet(String fieldname) {
if (lazyload == null) return;
if (lazyload.needLoad(fieldname) && lazyload.process(this, fieldname)) {
lazyload = null; // 清理掉,以后不再需要延迟加载
}
}
/*
* 供子类的equals方法调用,判断内嵌的query对象、updateMap对象是否相等
*/
protected final boolean isEquals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
DBObject that = (DBObject) obj;
if (_recordUpdate != that._recordUpdate) return false;
if (updateValueMap != null ? !updateValueMap.equals(that.updateValueMap) : that.updateValueMap != null)
return false;
return true;
}
@Override
public void touchUsedFlag(DBField field, boolean flag) {
if (flag) {
if (updateValueMap == null) updateValueMap = new TreeMap(cmp);
if (updateValueMap.containsKey(field)) {
return;
}
TableMapping meta = MetaHolder.getMeta(this.getClass());
ColumnMapping ba = meta.getSchemaMap().get(field);
updateValueMap.put(field, ba.getFieldAccessor().get(this));
} else {
if (updateValueMap != null) {
updateValueMap.remove(field);
}
}
}
public Map getTail() {
return tail;
}
public void setTail(Map tail) {
this.tail = tail;
}
/**
* 填充非空值到 updateValueMap 中
* 注意:使用该方法需要确保bean使用包装类且不包含有默认值的数据库字段.
*/
public void fillNotNullValue() {
OrmUtils.fillNotNullValue(this);
}
@Override
public void close() throws Exception {
}
/**
* 获取字段索引
* @param fieldName
* @return
*/
@Override
public int fieldIndex(String fieldName) {
return 0;
}
/**
* 推
* 将一个值保存到类的属性中
* @param fieldIndex 属性名称
* @param value 属性值
* @return true 保存成功 false 保存失败
*/
@Override
public boolean push(int fieldIndex, Object value) {
return false;
}
@Override
public boolean push(String fieldName, Object value) {
return false;
}
/**
* 拉取
* 获取类的属性的值
* @param fieldIndex
* @return 有值则成功, 无值也不代表获取失败.
*/
@Override
public Object pull(int fieldIndex) {
return null;
}
@Override
public Object pull(String fieldName) {
return null;
}
private static class ConditionComparator implements Comparator, Serializable {
@Override
public int compare(DBField o1, DBField o2) {
if (o1 == o2) return 0;
if (o1 == null) return 1;
if (o2 == null) return -1;
return o1.name().compareTo(o2.name());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy