mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Test case for #83 - Bug: Stateless update cascading to OneToOne or
ManyToOne incorrectly tries to INSERT rather than UPDATE
This commit is contained in:
@@ -2,6 +2,7 @@ package com.avaje.tests.model.basic;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
@@ -14,7 +15,7 @@ public class UUTwo {
|
||||
|
||||
String name;
|
||||
|
||||
@ManyToOne
|
||||
@ManyToOne(cascade=CascadeType.PERSIST)
|
||||
UUOne master;
|
||||
|
||||
public UUID getId() {
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user