mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
68 lines
1.8 KiB
Java
68 lines
1.8 KiB
Java
package com.avaje.tests.update;
|
|
|
|
import org.junit.Assert;
|
|
import org.junit.Test;
|
|
|
|
import com.avaje.ebean.BaseTestCase;
|
|
import com.avaje.ebean.Ebean;
|
|
import com.avaje.ebean.text.json.JsonContext;
|
|
import com.avaje.ebean.text.json.JsonWriteOptions;
|
|
import com.avaje.tests.model.basic.UUOne;
|
|
import com.avaje.tests.model.basic.UUTwo;
|
|
|
|
public class TestJsonStatelessUpdate extends BaseTestCase {
|
|
|
|
@Test
|
|
public void test() {
|
|
|
|
UUOne one = new UUOne();
|
|
one.setName("oneName");
|
|
|
|
Ebean.save(one);
|
|
|
|
UUTwo two = new UUTwo();
|
|
two.setMaster(one);
|
|
two.setName("twoName");
|
|
|
|
Ebean.save(two);
|
|
|
|
UUTwo twoX = Ebean.find(UUTwo.class, two.getId());
|
|
|
|
JsonContext jsonContext = Ebean.createJsonContext();
|
|
|
|
JsonWriteOptions writeOptions = JsonWriteOptions.parsePath("(id,name,master(*))");
|
|
String jsonString = jsonContext.toJsonString(twoX, true, writeOptions);
|
|
|
|
System.out.println(jsonString);
|
|
jsonString = jsonString.replace("twoName", "twoNameModified");
|
|
jsonString = jsonString.replace("oneName", "oneNameModified");
|
|
|
|
|
|
UUTwo two2 = jsonContext.toBean(UUTwo.class, jsonString);
|
|
|
|
Assert.assertEquals(twoX.getId(), two2.getId());
|
|
Assert.assertEquals("twoNameModified", two2.getName());
|
|
Assert.assertEquals("oneNameModified", two2.getMaster().getName());
|
|
|
|
// The update below cascades to also save "master" and that fails
|
|
// as it thinks it should INSERT master rather than UPDATE master
|
|
|
|
// Ebean.update(two2);
|
|
|
|
|
|
// The following is a workaround, to explicitly update master first
|
|
// so then Ebean doesn't try to save it when two2 is updated
|
|
Ebean.beginTransaction();
|
|
try {
|
|
UUOne master = two2.getMaster();
|
|
Ebean.update(master);
|
|
Ebean.update(two2);
|
|
|
|
} finally {
|
|
Ebean.endTransaction();
|
|
}
|
|
|
|
}
|
|
|
|
}
|