diff --git a/src/main/java/org/redkale/persistence/Cacheable.java b/src/main/java/org/redkale/persistence/Cacheable.java
index 73b5f556f..da678cfe1 100644
--- a/src/main/java/org/redkale/persistence/Cacheable.java
+++ b/src/main/java/org/redkale/persistence/Cacheable.java
@@ -31,8 +31,11 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
* Cacheable(false) means that the entity and its state must
* not be cached by the provider.
*
+ * @deprecated replace by {@link org.redkale.persistence.Entity#cacheable() }
+ *
* @since Java Persistence 2.0
*/
+@Deprecated(since = "2.8.0")
@Target({TYPE})
@Retention(RUNTIME)
public @interface Cacheable {
diff --git a/src/main/java/org/redkale/persistence/Entity.java b/src/main/java/org/redkale/persistence/Entity.java
index d1e8be0e1..8539ec282 100644
--- a/src/main/java/org/redkale/persistence/Entity.java
+++ b/src/main/java/org/redkale/persistence/Entity.java
@@ -1,4 +1,4 @@
-/*******************************************************************************
+/** *****************************************************************************
* Copyright (c) 2008 - 2013 Oracle Corporation. All rights reserved.
*
* This program and the accompanying materials are made available under the
@@ -12,7 +12,7 @@
* Linda DeMichiel - Java Persistence 2.1
* Linda DeMichiel - Java Persistence 2.0
*
- ******************************************************************************/
+ ***************************************************************************** */
package org.redkale.persistence;
import java.lang.annotation.*;
@@ -22,7 +22,7 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* Specifies that the class is an entity. This annotation is applied to the
* entity class.
- *
+ *
* @since Java Persistence 1.0
*/
@Inherited
@@ -31,19 +31,41 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Retention(RUNTIME)
public @interface Entity {
- /**
- * (Optional) The entity name. Defaults to the unqualified
- * name of the entity class. This name is used to refer to the
- * entity in queries. The name must not be a reserved literal
- * in the Java Persistence query language.
+ /**
+ * (Optional) The entity name. Defaults to the unqualified
+ * name of the entity class. This name is used to refer to the
+ * entity in queries. The name must not be a reserved literal
+ * in the Java Persistence query language.
+ *
* @return String
- */
- String name() default "";
-
+ */
+ String name() default "";
+
/**
* (Optional) The comment of the entity.
*
* @return String
*/
String comment() default "";
+
+ /**
+ * (Optional) 是否缓存实体对象
+ *
+ * @return boolean
+ */
+ boolean cacheable() default false;
+
+ /**
+ * (Optional) 定时自动更新缓存的周期秒数,为0表示不做定时更新, 大于0表示每经过interval秒后会自动从数据库中拉取数据更新Cache
+ *
+ * @return int
+ */
+ int cacheInterval() default 0;
+
+ /**
+ * (Optional) DataSource是否直接返回对象的真实引用, 而不是copy一份
+ *
+ * @return boolean
+ */
+ boolean cacheDirect() default false;
}
diff --git a/src/main/java/org/redkale/source/EntityInfo.java b/src/main/java/org/redkale/source/EntityInfo.java
index 84b05bd63..bf9d4ee11 100644
--- a/src/main/java/org/redkale/source/EntityInfo.java
+++ b/src/main/java/org/redkale/source/EntityInfo.java
@@ -687,10 +687,37 @@ public final class EntityInfo {
this.updateQuestionPrepareCaseSQLs = null;
}
//----------------cache--------------
- Cacheable c1 = type.getAnnotation(Cacheable.class);
- javax.persistence.Cacheable c2 = type.getAnnotation(javax.persistence.Cacheable.class);
- if (this.table == null || (!cacheForbidden && c1 != null && c1.value()) || (!cacheForbidden && c2 != null && c2.value())) {
- this.cache = new EntityCache<>(this, c1 == null ? (c2 == null ? 0 : c2.interval()) : c1.interval(), c1 == null ? (c2 != null && c2.direct()) : c1.direct());
+ boolean cacheable = false;
+ int interval = 0;
+ boolean direct = false;
+ org.redkale.persistence.Entity en = type.getAnnotation(org.redkale.persistence.Entity.class);
+ if (en != null) {
+ cacheable = en.cacheable();
+ interval = en.cacheInterval();
+ direct = en.cacheDirect();
+ } else {
+ org.redkale.persistence.VirtualEntity ve = type.getAnnotation(org.redkale.persistence.VirtualEntity.class);
+ if (ve != null) {
+ cacheable = true;
+ direct = ve.direct();
+ }
+ }
+ { //兼容旧类
+ org.redkale.persistence.Cacheable c1 = type.getAnnotation(org.redkale.persistence.Cacheable.class);
+ if (c1 != null) {
+ cacheable = c1.value();
+ interval = c1.interval();
+ direct = c1.direct();
+ }
+ javax.persistence.Cacheable c2 = type.getAnnotation(javax.persistence.Cacheable.class);
+ if (c2 != null) {
+ cacheable = c2.value();
+ interval = c2.interval();
+ direct = c2.direct();
+ }
+ }
+ if (this.table == null || (!cacheForbidden && cacheable)) {
+ this.cache = new EntityCache<>(this, interval, direct);
} else {
this.cache = null;
}
diff --git a/src/test/java/org/redkale/test/source/FilterNodeTest.java b/src/test/java/org/redkale/test/source/FilterNodeTest.java
index f15bd48af..071e1def7 100644
--- a/src/test/java/org/redkale/test/source/FilterNodeTest.java
+++ b/src/test/java/org/redkale/test/source/FilterNodeTest.java
@@ -12,7 +12,7 @@ import java.util.function.*;
import org.junit.jupiter.api.*;
import org.redkale.annotation.AutoLoad;
import org.redkale.convert.json.*;
-import org.redkale.persistence.Cacheable;
+import org.redkale.persistence.Entity;
import org.redkale.persistence.Id;
import org.redkale.persistence.Transient;
import org.redkale.source.*;
@@ -187,7 +187,7 @@ public class FilterNodeTest {
}
@AutoLoad
- @Cacheable
+ @Entity(cacheable = true)
public static class CarTestTable {
public static List createList() {
@@ -280,7 +280,7 @@ public class FilterNodeTest {
}
@AutoLoad
- @Cacheable
+ @Entity(cacheable = true)
public static class CarTypeTable {
public static List createList() {
@@ -337,7 +337,7 @@ public class FilterNodeTest {
}
@AutoLoad
- @Cacheable
+ @Entity(cacheable = true)
public static class UserTestTable {
public static List createList() {
diff --git a/src/test/java/org/redkale/test/source/TestSourceCache.java b/src/test/java/org/redkale/test/source/TestSourceCache.java
index 43538fde6..8dd3f3c68 100644
--- a/src/test/java/org/redkale/test/source/TestSourceCache.java
+++ b/src/test/java/org/redkale/test/source/TestSourceCache.java
@@ -86,7 +86,6 @@ public class TestSourceCache {
}
@VirtualEntity
- @Cacheable
public static class TestEntity {
@Id