This commit is contained in:
@@ -11,7 +11,7 @@ import java.util.concurrent.*;
|
|||||||
import java.util.function.*;
|
import java.util.function.*;
|
||||||
import java.util.logging.*;
|
import java.util.logging.*;
|
||||||
import java.util.stream.*;
|
import java.util.stream.*;
|
||||||
import javax.persistence.Transient;
|
import javax.persistence.*;
|
||||||
import static org.redkale.source.FilterFunc.*;
|
import static org.redkale.source.FilterFunc.*;
|
||||||
import org.redkale.util.*;
|
import org.redkale.util.*;
|
||||||
|
|
||||||
@@ -42,7 +42,9 @@ public final class EntityCache<T> {
|
|||||||
|
|
||||||
private final Attribute<T, Serializable> primary;
|
private final Attribute<T, Serializable> primary;
|
||||||
|
|
||||||
private final Reproduce<T, T> reproduce;
|
private final Reproduce<T, T> newReproduce;
|
||||||
|
|
||||||
|
private final Reproduce<T, T> chgReproduce;
|
||||||
|
|
||||||
private volatile boolean fullloaded;
|
private volatile boolean fullloaded;
|
||||||
|
|
||||||
@@ -54,13 +56,24 @@ public final class EntityCache<T> {
|
|||||||
this.creator = info.getCreator();
|
this.creator = info.getCreator();
|
||||||
this.primary = info.primary;
|
this.primary = info.primary;
|
||||||
this.needcopy = true;
|
this.needcopy = true;
|
||||||
this.reproduce = Reproduce.create(type, type, (m) -> {
|
this.newReproduce = Reproduce.create(type, type, (m) -> {
|
||||||
try {
|
try {
|
||||||
return type.getDeclaredField(m).getAnnotation(Transient.class) == null;
|
return type.getDeclaredField(m).getAnnotation(Transient.class) == null;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
this.chgReproduce = Reproduce.create(type, type, (m) -> {
|
||||||
|
try {
|
||||||
|
java.lang.reflect.Field field = type.getDeclaredField(m);
|
||||||
|
if(field.getAnnotation(Transient.class) != null) return false;
|
||||||
|
Column column = field.getAnnotation(Column.class);
|
||||||
|
if(column != null && !column.updatable()) return false;
|
||||||
|
return true;
|
||||||
|
} catch (Exception e) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void fullLoad() {
|
public void fullLoad() {
|
||||||
@@ -91,14 +104,14 @@ public final class EntityCache<T> {
|
|||||||
public T find(Serializable id) {
|
public T find(Serializable id) {
|
||||||
if (id == null) return null;
|
if (id == null) return null;
|
||||||
T rs = map.get(id);
|
T rs = map.get(id);
|
||||||
return rs == null ? null : (needcopy ? reproduce.copy(this.creator.create(), rs) : rs);
|
return rs == null ? null : (needcopy ? newReproduce.copy(this.creator.create(), rs) : rs);
|
||||||
}
|
}
|
||||||
|
|
||||||
public T find(final SelectColumn selects, final Serializable id) {
|
public T find(final SelectColumn selects, final Serializable id) {
|
||||||
if (id == null) return null;
|
if (id == null) return null;
|
||||||
T rs = map.get(id);
|
T rs = map.get(id);
|
||||||
if (rs == null) return null;
|
if (rs == null) return null;
|
||||||
if (selects == null) return (needcopy ? reproduce.copy(this.creator.create(), rs) : rs);
|
if (selects == null) return (needcopy ? newReproduce.copy(this.creator.create(), rs) : rs);
|
||||||
T t = this.creator.create();
|
T t = this.creator.create();
|
||||||
for (Attribute attr : this.info.attributes) {
|
for (Attribute attr : this.info.attributes) {
|
||||||
if (selects.test(attr.field())) attr.set(t, attr.get(rs));
|
if (selects.test(attr.field())) attr.set(t, attr.get(rs));
|
||||||
@@ -112,7 +125,7 @@ public final class EntityCache<T> {
|
|||||||
if (filter != null) stream = stream.filter(filter);
|
if (filter != null) stream = stream.filter(filter);
|
||||||
Optional<T> opt = stream.findFirst();
|
Optional<T> opt = stream.findFirst();
|
||||||
if (!opt.isPresent()) return null;
|
if (!opt.isPresent()) return null;
|
||||||
if (selects == null) return (needcopy ? reproduce.copy(this.creator.create(), opt.get()) : opt.get());
|
if (selects == null) return (needcopy ? newReproduce.copy(this.creator.create(), opt.get()) : opt.get());
|
||||||
T rs = opt.get();
|
T rs = opt.get();
|
||||||
T t = this.creator.create();
|
T t = this.creator.create();
|
||||||
for (Attribute attr : this.info.attributes) {
|
for (Attribute attr : this.info.attributes) {
|
||||||
@@ -292,7 +305,7 @@ public final class EntityCache<T> {
|
|||||||
if (flipper != null) stream = stream.skip(flipper.index()).limit(flipper.getSize());
|
if (flipper != null) stream = stream.skip(flipper.index()).limit(flipper.getSize());
|
||||||
final List<T> rs = new ArrayList<>();
|
final List<T> rs = new ArrayList<>();
|
||||||
if (selects == null) {
|
if (selects == null) {
|
||||||
Consumer<? super T> action = x -> rs.add(needcopy ? reproduce.copy(creator.create(), x) : x);
|
Consumer<? super T> action = x -> rs.add(needcopy ? newReproduce.copy(creator.create(), x) : x);
|
||||||
if (comparator != null) {
|
if (comparator != null) {
|
||||||
stream.forEachOrdered(action);
|
stream.forEachOrdered(action);
|
||||||
} else {
|
} else {
|
||||||
@@ -322,7 +335,7 @@ public final class EntityCache<T> {
|
|||||||
|
|
||||||
public void insert(T value) {
|
public void insert(T value) {
|
||||||
if (value == null) return;
|
if (value == null) return;
|
||||||
final T rs = reproduce.copy(this.creator.create(), value); //确保同一主键值的map与list中的对象必须共用。
|
final T rs = newReproduce.copy(this.creator.create(), value); //确保同一主键值的map与list中的对象必须共用。
|
||||||
T old = this.map.put(this.primary.get(rs), rs);
|
T old = this.map.put(this.primary.get(rs), rs);
|
||||||
if (old == null) {
|
if (old == null) {
|
||||||
this.list.add(rs);
|
this.list.add(rs);
|
||||||
@@ -355,7 +368,7 @@ public final class EntityCache<T> {
|
|||||||
if (value == null) return;
|
if (value == null) return;
|
||||||
T rs = this.map.get(this.primary.get(value));
|
T rs = this.map.get(this.primary.get(value));
|
||||||
if (rs == null) return;
|
if (rs == null) return;
|
||||||
this.reproduce.copy(rs, value);
|
this.chgReproduce.copy(rs, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public T update(final T value, Collection<Attribute<T, Serializable>> attrs) {
|
public T update(final T value, Collection<Attribute<T, Serializable>> attrs) {
|
||||||
|
|||||||
Reference in New Issue
Block a user