LiveObjectService merge method fixed.

Deny to allow persist object with transient REntity
pull/653/head
Nikita 8 years ago
parent 000fb85375
commit ac7aef4706

@ -174,6 +174,7 @@ public class RedissonLiveObjectService implements RLiveObjectService {
@Override @Override
public <T> T merge(T detachedObject) { public <T> T merge(T detachedObject) {
T attachedObject = attach(detachedObject); T attachedObject = attach(detachedObject);
getMap(attachedObject).fastPut("redisson_live_object", "1");
copy(detachedObject, attachedObject); copy(detachedObject, attachedObject);
return attachedObject; return attachedObject;
} }
@ -388,6 +389,13 @@ public class RedissonLiveObjectService implements RLiveObjectService {
} }
private <T> void copy(T detachedObject, T attachedObject) { private <T> 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())) { // for (FieldDescription.InDefinedShape field : Introspectior.getFieldsDescription(detachedObject.getClass())) {
// Object obj = ClassUtils.getField(detachedObject, field.getName()); // Object obj = ClassUtils.getField(detachedObject, field.getName());
// if (obj instanceof SortedSet) { // if (obj instanceof SortedSet) {

@ -28,11 +28,11 @@ import net.bytebuddy.matcher.ElementMatchers;
*/ */
public class Introspectior { public class Introspectior {
public static TypeDescription.ForLoadedType getTypeDescription(Class c) { public static TypeDescription.ForLoadedType getTypeDescription(Class<?> c) {
return new TypeDescription.ForLoadedType(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()) { if (method == null || method.isEmpty()) {
return null; return null;
} }
@ -42,7 +42,13 @@ public class Introspectior {
.getOnly(); .getOnly();
} }
public static FieldDescription getFieldDescription(Class c, String field) { public static FieldList<FieldDescription.InDefinedShape> getFieldsDescription(Class<?> c) {
return getTypeDescription(c)
.getDeclaredFields();
}
public static FieldDescription getFieldDescription(Class<?> c, String field) {
if (field == null || field.isEmpty()) { if (field == null || field.isEmpty()) {
return null; return null;
} }
@ -52,7 +58,7 @@ public class Introspectior {
.getOnly(); .getOnly();
} }
public static FieldList<FieldDescription.InDefinedShape> getFieldsWithAnnotation(Class c, Class<? extends Annotation> a) { public static FieldList<FieldDescription.InDefinedShape> getFieldsWithAnnotation(Class<?> c, Class<? extends Annotation> a) {
return getTypeDescription(c) return getTypeDescription(c)
.getDeclaredFields() .getDeclaredFields()
.filter(ElementMatchers.isAnnotatedWith(a)); .filter(ElementMatchers.isAnnotatedWith(a));

@ -579,6 +579,7 @@ public class RedissonLiveObjectServiceTest extends BaseTest {
assertEquals("VALUE", merged.getValue()); assertEquals("VALUE", merged.getValue());
try { try {
service.persist(ts); service.persist(ts);
fail("Should not be here");
} catch (Exception e) { } catch (Exception e) {
assertEquals("This REntity already exists.", e.getMessage()); assertEquals("This REntity already exists.", e.getMessage());
} }
@ -1188,6 +1189,14 @@ public class RedissonLiveObjectServiceTest extends BaseTest {
private Customer customer; private Customer customer;
public Order() {
}
public Order(Customer customer) {
super();
this.customer = customer;
}
public void setCustomer(Customer customer) { public void setCustomer(Customer customer) {
this.customer = customer; this.customer = customer;
} }
@ -1210,6 +1219,13 @@ public class RedissonLiveObjectServiceTest extends BaseTest {
customer.getOrders().add(order); 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 @Test
public void testObjectShouldBeAttached() { public void testObjectShouldBeAttached() {
Customer customer = new Customer("12"); Customer customer = new Customer("12");
@ -1261,7 +1277,6 @@ public class RedissonLiveObjectServiceTest extends BaseTest {
private String name; private String name;
protected ClassWithoutIdSetterGetter() { protected ClassWithoutIdSetterGetter() {
System.out.println("123");
} }
public ClassWithoutIdSetterGetter(String name) { public ClassWithoutIdSetterGetter(String name) {

Loading…
Cancel
Save