diff --git a/redisson/src/main/java/org/redisson/RedissonLiveObjectService.java b/redisson/src/main/java/org/redisson/RedissonLiveObjectService.java index 1bad81e49..414880c92 100644 --- a/redisson/src/main/java/org/redisson/RedissonLiveObjectService.java +++ b/redisson/src/main/java/org/redisson/RedissonLiveObjectService.java @@ -174,6 +174,7 @@ public class RedissonLiveObjectService implements RLiveObjectService { @Override public T merge(T detachedObject) { T attachedObject = attach(detachedObject); + getMap(attachedObject).fastPut("redisson_live_object", "1"); copy(detachedObject, attachedObject); return attachedObject; } @@ -388,6 +389,13 @@ public class RedissonLiveObjectService implements RLiveObjectService { } private void copy(T detachedObject, T attachedObject) { + for (FieldDescription.InDefinedShape field : Introspectior.getFieldsDescription(detachedObject.getClass())) { + Object object = ClassUtils.getField(detachedObject, field.getName()); + if (object != null && object.getClass().isAnnotationPresent(REntity.class)) { + throw new IllegalArgumentException("REntity should be attached to Redisson before save"); + } + + } // for (FieldDescription.InDefinedShape field : Introspectior.getFieldsDescription(detachedObject.getClass())) { // Object obj = ClassUtils.getField(detachedObject, field.getName()); // if (obj instanceof SortedSet) { diff --git a/redisson/src/main/java/org/redisson/liveobject/misc/Introspectior.java b/redisson/src/main/java/org/redisson/liveobject/misc/Introspectior.java index c13c398ff..01c9f9d3a 100644 --- a/redisson/src/main/java/org/redisson/liveobject/misc/Introspectior.java +++ b/redisson/src/main/java/org/redisson/liveobject/misc/Introspectior.java @@ -28,11 +28,11 @@ import net.bytebuddy.matcher.ElementMatchers; */ public class Introspectior { - public static TypeDescription.ForLoadedType getTypeDescription(Class c) { + public static TypeDescription.ForLoadedType getTypeDescription(Class c) { return new TypeDescription.ForLoadedType(c); } - public static MethodDescription getMethodDescription(Class c, String method) { + public static MethodDescription getMethodDescription(Class c, String method) { if (method == null || method.isEmpty()) { return null; } @@ -42,7 +42,13 @@ public class Introspectior { .getOnly(); } - public static FieldDescription getFieldDescription(Class c, String field) { + public static FieldList getFieldsDescription(Class c) { + return getTypeDescription(c) + .getDeclaredFields(); + + } + + public static FieldDescription getFieldDescription(Class c, String field) { if (field == null || field.isEmpty()) { return null; } @@ -52,7 +58,7 @@ public class Introspectior { .getOnly(); } - public static FieldList getFieldsWithAnnotation(Class c, Class a) { + public static FieldList getFieldsWithAnnotation(Class c, Class a) { return getTypeDescription(c) .getDeclaredFields() .filter(ElementMatchers.isAnnotatedWith(a)); diff --git a/redisson/src/test/java/org/redisson/RedissonLiveObjectServiceTest.java b/redisson/src/test/java/org/redisson/RedissonLiveObjectServiceTest.java index 1e027cec6..d84634e51 100644 --- a/redisson/src/test/java/org/redisson/RedissonLiveObjectServiceTest.java +++ b/redisson/src/test/java/org/redisson/RedissonLiveObjectServiceTest.java @@ -579,6 +579,7 @@ public class RedissonLiveObjectServiceTest extends BaseTest { assertEquals("VALUE", merged.getValue()); try { service.persist(ts); + fail("Should not be here"); } catch (Exception e) { assertEquals("This REntity already exists.", e.getMessage()); } @@ -1188,6 +1189,14 @@ public class RedissonLiveObjectServiceTest extends BaseTest { private Customer customer; + public Order() { + } + + public Order(Customer customer) { + super(); + this.customer = customer; + } + public void setCustomer(Customer customer) { this.customer = customer; } @@ -1210,6 +1219,13 @@ public class RedissonLiveObjectServiceTest extends BaseTest { customer.getOrders().add(order); } + @Test(expected = IllegalArgumentException.class) + public void testObjectShouldNotBeAttached2() { + Customer customer = new Customer("12"); + Order order = new Order(customer); + order = redisson.getLiveObjectService().persist(order); + } + @Test public void testObjectShouldBeAttached() { Customer customer = new Customer("12"); @@ -1261,7 +1277,6 @@ public class RedissonLiveObjectServiceTest extends BaseTest { private String name; protected ClassWithoutIdSetterGetter() { - System.out.println("123"); } public ClassWithoutIdSetterGetter(String name) {