mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#919 - io.ebean package initial
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package org.tests.update;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import org.tests.model.embedded.EEmbDatePeriod;
|
||||
import org.tests.model.embedded.EEmbInner;
|
||||
import org.tests.model.embedded.EEmbOuter;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestEmbeddedRefreshUpdate extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
EEmbOuter outer = new EEmbOuter();
|
||||
outer.setNomeOuter("test");
|
||||
|
||||
EEmbDatePeriod embeddedBean = new EEmbDatePeriod();
|
||||
embeddedBean.setDate1(new Date());
|
||||
|
||||
outer.setDatePeriod(embeddedBean);
|
||||
|
||||
Ebean.save(outer);
|
||||
|
||||
EEmbOuter loaded = Ebean.find(EEmbOuter.class).where().idEq(outer.getId()).findUnique();
|
||||
|
||||
// if commented Ebean saves correctly
|
||||
Ebean.refresh(loaded);
|
||||
|
||||
loaded.getDatePeriod().setDate2(new Date());
|
||||
|
||||
// BUG 343
|
||||
Ebean.save(loaded);
|
||||
|
||||
// See BUG 344
|
||||
Ebean.find(EEmbInner.class).fetch("outer").orderBy("outer.datePeriod.date1").findList();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test2() {
|
||||
|
||||
EEmbOuter outer = new EEmbOuter();
|
||||
outer.setNomeOuter("test");
|
||||
EEmbDatePeriod embeddedBean = new EEmbDatePeriod();
|
||||
embeddedBean.setDate1(new Date());
|
||||
outer.setDatePeriod(embeddedBean);
|
||||
|
||||
Ebean.save(outer);
|
||||
assertThat(outer.getUpdateCount()).isEqualTo(1);
|
||||
|
||||
Date d = new Date();
|
||||
d.setTime(1L);
|
||||
|
||||
outer.getDatePeriod().setDate1(d);
|
||||
Ebean.save(outer);
|
||||
assertThat(outer.getUpdateCount()).isEqualTo(2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package org.tests.update;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.text.json.JsonContext;
|
||||
import io.ebean.text.json.JsonWriteOptions;
|
||||
import org.tests.model.basic.UUOne;
|
||||
import org.tests.model.basic.UUTwo;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class TestJsonStatelessUpdate extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() throws IOException {
|
||||
|
||||
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.json();
|
||||
|
||||
JsonWriteOptions writeOptions = JsonWriteOptions.parsePath("(id,name,master(*))");
|
||||
String jsonString = jsonContext.toJson(twoX, writeOptions);
|
||||
|
||||
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);
|
||||
awaitL2Cache();
|
||||
|
||||
|
||||
// confirm the properties where updated as expected
|
||||
UUTwo twoConfirm = Ebean.find(UUTwo.class, two.getId());
|
||||
|
||||
Assert.assertEquals("twoNameModified", twoConfirm.getName());
|
||||
Assert.assertEquals("oneNameModified", twoConfirm.getMaster().getName());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package org.tests.update;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import org.tests.model.basic.EBasicVer;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
|
||||
public class TestMarkAsDirty extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() throws InterruptedException {
|
||||
|
||||
EBasicVer bean = new EBasicVer("markAsDirty");
|
||||
Ebean.save(bean);
|
||||
|
||||
Timestamp lastUpdate = bean.getLastUpdate();
|
||||
Assert.assertNotNull(lastUpdate);
|
||||
|
||||
Thread.sleep(100);
|
||||
|
||||
// ensure the update occurs and version property is updated/incremented
|
||||
Ebean.markAsDirty(bean);
|
||||
Ebean.save(bean);
|
||||
|
||||
Timestamp lastUpdate2 = bean.getLastUpdate();
|
||||
Assert.assertNotNull(lastUpdate2);
|
||||
Assert.assertNotEquals(lastUpdate, lastUpdate2);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package org.tests.update;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestPostgresTruncate extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
Assert.assertTrue(true);
|
||||
// EbeanServer server = Ebean.getServer(null);
|
||||
//
|
||||
// EBasic e = new EBasic();
|
||||
// e.setName("something");
|
||||
// e.setStatus(Status.NEW);
|
||||
// e.setDescription("wow");
|
||||
//
|
||||
// server.save(e);
|
||||
//
|
||||
// server.beginTransaction();
|
||||
// Integer oriC =
|
||||
// Ebean.createSqlQuery("select count(*) as c from e_basic").findUnique().getInteger("c");
|
||||
// int rows = Ebean.createSqlUpdate("truncate e_basic cascade").execute();
|
||||
// Integer currentC =
|
||||
// Ebean.createSqlQuery("select count(*) as c from e_basic").findUnique().getInteger("c");
|
||||
// server.commitTransaction();
|
||||
// //Ebean.getServerCacheManager().clearAll();
|
||||
// System.out.println("table : ori="+oriC+", delC="+rows+", currentC="+currentC);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package org.tests.update;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.SqlUpdate;
|
||||
import org.junit.Test;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class TestSqlUpdateBindMultipleLists extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
SqlUpdate sqlUpdate = Ebean.createSqlUpdate("delete from o_customer where id in (:ids)");
|
||||
|
||||
sqlUpdate.setParameter("ids", asList(9991, 9992, 9993));
|
||||
sqlUpdate.execute();
|
||||
assertEquals("delete from o_customer where id in (?,?,?)", sqlUpdate.getGeneratedSql());
|
||||
|
||||
// 3 parameters in the IN clause
|
||||
sqlUpdate.setParameter("ids", asList(9991, 9992));
|
||||
sqlUpdate.execute();
|
||||
assertEquals("delete from o_customer where id in (?,?)", sqlUpdate.getGeneratedSql());
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void test_multipleLists() {
|
||||
|
||||
SqlUpdate sqlUpdate = Ebean.createSqlUpdate("delete from o_customer where id in (:ids) and name in (:names)");
|
||||
|
||||
sqlUpdate.setParameter("ids", asList(9991, 9992, 9993));
|
||||
sqlUpdate.setParameter("names", asList("rob", "jim"));
|
||||
sqlUpdate.execute();
|
||||
assertEquals("delete from o_customer where id in (?,?,?) and name in (?,?)", sqlUpdate.getGeneratedSql());
|
||||
|
||||
sqlUpdate.setParameter("ids", asList(9991, 9992));
|
||||
sqlUpdate.setParameter("names", asList("rob", "jim", "sd"));
|
||||
sqlUpdate.execute();
|
||||
assertEquals("delete from o_customer where id in (?,?) and name in (?,?,?)", sqlUpdate.getGeneratedSql());
|
||||
|
||||
sqlUpdate.setParameter("ids", asList(9991, 9992));
|
||||
sqlUpdate.setParameter("names", asList("rob", "jim"));
|
||||
sqlUpdate.execute();
|
||||
assertEquals("delete from o_customer where id in (?,?) and name in (?,?)", sqlUpdate.getGeneratedSql());
|
||||
|
||||
sqlUpdate.setParameter("ids", asList(9991));
|
||||
sqlUpdate.setParameter("names", asList("rob", "jim"));
|
||||
sqlUpdate.execute();
|
||||
assertEquals("delete from o_customer where id in (?) and name in (?,?)", sqlUpdate.getGeneratedSql());
|
||||
|
||||
sqlUpdate.setParameter("ids", asList(9992));
|
||||
sqlUpdate.setParameter("names", asList("ro3b", "j3im"));
|
||||
sqlUpdate.execute();
|
||||
assertEquals("delete from o_customer where id in (?) and name in (?,?)", sqlUpdate.getGeneratedSql());
|
||||
|
||||
sqlUpdate.setParameter("ids", asList(9992, 4545));
|
||||
sqlUpdate.setParameter("names", asList("ro3b"));
|
||||
sqlUpdate.execute();
|
||||
assertEquals("delete from o_customer where id in (?,?) and name in (?)", sqlUpdate.getGeneratedSql());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package org.tests.update;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.SqlUpdate;
|
||||
import org.tests.idkeys.db.AuditLog;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestSqlUpdateInTxn extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testBasic() {
|
||||
|
||||
AuditLog otherLog = new AuditLog();
|
||||
otherLog.setDescription("foo");
|
||||
Ebean.save(otherLog);
|
||||
|
||||
AuditLog log = new AuditLog();
|
||||
log.setDescription("foo");
|
||||
|
||||
Ebean.save(log);
|
||||
|
||||
AuditLog log2 = Ebean.find(AuditLog.class, log.getId());
|
||||
Assert.assertEquals("foo", log2.getDescription());
|
||||
|
||||
final Long id = log2.getId();
|
||||
final String updateDml = "update audit_log set description = :desc where id = :id";
|
||||
final String updateModDml = "update audit_log set modified_description = :desc";
|
||||
|
||||
SqlUpdate sqlUpdate = Ebean.createSqlUpdate(updateDml);
|
||||
sqlUpdate.setParameter("desc", "foo2");
|
||||
sqlUpdate.setParameter("id", id);
|
||||
sqlUpdate.execute();
|
||||
|
||||
SqlUpdate updateMod = Ebean.createSqlUpdate(updateModDml);
|
||||
updateMod.setParameter("desc", "mod0");
|
||||
updateMod.execute();
|
||||
|
||||
AuditLog log3 = Ebean.find(AuditLog.class, log.getId());
|
||||
Assert.assertEquals("foo2", log3.getDescription());
|
||||
Assert.assertEquals("mod0", log3.getModifiedDescription());
|
||||
|
||||
Ebean.execute(() -> {
|
||||
SqlUpdate update = Ebean.createSqlUpdate(updateDml);
|
||||
update.setParameter("desc", "foo3");
|
||||
update.setParameter("id", id);
|
||||
update.execute();
|
||||
|
||||
SqlUpdate updateMod1 = Ebean.createSqlUpdate(updateModDml);
|
||||
updateMod1.setParameter("desc", "mod1");
|
||||
updateMod1.execute();
|
||||
});
|
||||
|
||||
AuditLog log4 = Ebean.find(AuditLog.class, log.getId());
|
||||
Assert.assertEquals("foo3", log4.getDescription());
|
||||
Assert.assertEquals("mod1", log4.getModifiedDescription());
|
||||
|
||||
|
||||
Ebean.beginTransaction();
|
||||
|
||||
SqlUpdate update = Ebean.createSqlUpdate(updateDml);
|
||||
update.setParameter("desc", "foo4");
|
||||
update.setParameter("id", id);
|
||||
update.execute();
|
||||
|
||||
updateMod = Ebean.createSqlUpdate(updateModDml);
|
||||
updateMod.setParameter("desc", "mod2");
|
||||
updateMod.execute();
|
||||
|
||||
Ebean.commitTransaction();
|
||||
|
||||
AuditLog log5 = Ebean.find(AuditLog.class, log.getId());
|
||||
Assert.assertEquals("foo4", log5.getDescription());
|
||||
Assert.assertEquals("mod2", log5.getModifiedDescription());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,386 @@
|
||||
package org.tests.update;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.EbeanServer;
|
||||
import org.tests.model.basic.Contact;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.tests.model.basic.EBasic;
|
||||
import org.tests.model.basic.EBasic.Status;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
public class TestStatelessUpdate extends BaseTestCase {
|
||||
|
||||
private EbeanServer server = server();
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
EBasic e = new EBasic();
|
||||
e.setName("something");
|
||||
e.setStatus(Status.NEW);
|
||||
e.setDescription("wow");
|
||||
|
||||
server.save(e);
|
||||
|
||||
// confirm saved as expected
|
||||
EBasic eBasic = server.find(EBasic.class, e.getId());
|
||||
assertEquals(e.getId(), eBasic.getId());
|
||||
assertEquals(e.getName(), eBasic.getName());
|
||||
assertEquals(e.getStatus(), eBasic.getStatus());
|
||||
assertEquals(e.getDescription(), eBasic.getDescription());
|
||||
|
||||
// test updating just the name
|
||||
EBasic updateAll = new EBasic();
|
||||
updateAll.setId(e.getId());
|
||||
updateAll.setName("updAllProps");
|
||||
|
||||
server.update(updateAll, null, false);
|
||||
|
||||
eBasic = server.find(EBasic.class, e.getId());
|
||||
assertEquals(e.getStatus(), eBasic.getStatus());
|
||||
assertEquals(e.getDescription(), eBasic.getDescription());
|
||||
assertEquals(updateAll.getName(), eBasic.getName());
|
||||
|
||||
|
||||
// test setting null
|
||||
EBasic updateDeflt = new EBasic();
|
||||
updateDeflt.setId(e.getId());
|
||||
updateDeflt.setName("updateDeflt");
|
||||
updateDeflt.setDescription(null);
|
||||
server.update(updateDeflt);
|
||||
|
||||
// name and description changed (using null)
|
||||
eBasic = server.find(EBasic.class, e.getId());
|
||||
assertEquals(e.getStatus(), eBasic.getStatus());
|
||||
assertEquals(updateDeflt.getName(), eBasic.getName());
|
||||
assertNull(eBasic.getDescription());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* I am expecting that Ebean detects there aren't any changes and don't execute any query.
|
||||
* Currently a {@link javax.persistence.PersistenceException} with message 'Invalid value "null" for parameter "SQL"' is thrown.
|
||||
*/
|
||||
@Test
|
||||
public void testWithoutChangesAndIgnoreNullValues() {
|
||||
// arrange
|
||||
EBasic basic = new EBasic();
|
||||
basic.setName("something");
|
||||
basic.setStatus(Status.NEW);
|
||||
basic.setDescription("wow");
|
||||
|
||||
server.save(basic);
|
||||
|
||||
// act
|
||||
EBasic basicWithoutChanges = new EBasic();
|
||||
basicWithoutChanges.setId(basic.getId());
|
||||
server.update(basicWithoutChanges);
|
||||
|
||||
// assert
|
||||
// Nothing to check, simply no exception should occur
|
||||
// maybe ensure that no update has been executed
|
||||
}
|
||||
|
||||
/**
|
||||
* Nice to have:
|
||||
* <br />
|
||||
* Assuming we have a Version column, it will always be generated an Update despite we have nothing to update.
|
||||
* It would be nice that this would be recognized and no update would happen.
|
||||
* <br />
|
||||
* <br />
|
||||
* This feature already works for normal Updates!
|
||||
* <br />
|
||||
* see: {@link org.tests.update.TestUpdatePartial#testWithoutChangesAndVersionColumn()}
|
||||
*/
|
||||
@Test
|
||||
public void testWithoutChangesAndVersionColumnAndIgnoreNullValues() {
|
||||
// arrange
|
||||
Customer customer = new Customer();
|
||||
customer.setName("something");
|
||||
|
||||
server.save(customer);
|
||||
|
||||
// act
|
||||
Customer customerWithoutChanges = new Customer();
|
||||
customerWithoutChanges.setId(customer.getId());
|
||||
server.update(customerWithoutChanges);
|
||||
|
||||
Customer result = Ebean.find(Customer.class, customer.getId());
|
||||
|
||||
// assert
|
||||
assertEquals(customer.getUpdtime().getTime(), result.getUpdtime().getTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* Many relations mustn't be deleted when they are not loaded.
|
||||
*/
|
||||
@Test
|
||||
public void testStatelessUpdateIgnoreNullCollection() {
|
||||
|
||||
// arrange
|
||||
Contact contact = new Contact();
|
||||
contact.setFirstName("wobu :P");
|
||||
|
||||
Customer customer = new Customer();
|
||||
customer.setName("something");
|
||||
customer.setContacts(new ArrayList<>());
|
||||
customer.getContacts().add(contact);
|
||||
|
||||
server.save(customer);
|
||||
|
||||
// act
|
||||
Customer customerWithChange = new Customer();
|
||||
customerWithChange.setId(customer.getId());
|
||||
customerWithChange.setName("new name");
|
||||
|
||||
// contacts is not loaded
|
||||
Assert.assertFalse(containsContacts(customerWithChange));
|
||||
server.update(customerWithChange);
|
||||
|
||||
Customer result = Ebean.find(Customer.class, customer.getId());
|
||||
|
||||
// assert null list was ignored (missing children not deleted)
|
||||
Assert.assertNotNull(result.getContacts());
|
||||
Assert.assertFalse("the contacts mustn't be deleted", result.getContacts().isEmpty());
|
||||
}
|
||||
|
||||
/**
|
||||
* When BeanCollection is inadvertantly initialised and empty then ignore it
|
||||
* Specifically a non-BeanCollection (like ArrayList) is not ignored in terms
|
||||
* of deleting missing children.
|
||||
*/
|
||||
@Test
|
||||
public void testStatelessUpdateIgnoreEmptyBeanCollection() {
|
||||
|
||||
// arrange
|
||||
Contact contact = new Contact();
|
||||
contact.setFirstName("wobu :P");
|
||||
|
||||
Customer customer = new Customer();
|
||||
customer.setName("something");
|
||||
customer.setContacts(new ArrayList<>());
|
||||
customer.getContacts().add(contact);
|
||||
|
||||
server.save(customer);
|
||||
|
||||
// act
|
||||
Customer customerWithChange = new Customer();
|
||||
customerWithChange.setId(customer.getId());
|
||||
customerWithChange.setName("new name");
|
||||
|
||||
// with Ebean enhancement this loads the an empty contacts BeanList
|
||||
customerWithChange.getContacts();
|
||||
|
||||
// contacts has been initialised to empty BeanList
|
||||
Assert.assertTrue(containsContacts(customerWithChange));
|
||||
server.update(customerWithChange);
|
||||
|
||||
Customer result = Ebean.find(Customer.class, customer.getId());
|
||||
|
||||
// assert empty bean list was ignore (missing children not deleted)
|
||||
Assert.assertNotNull(result.getContacts());
|
||||
Assert.assertFalse("the contacts mustn't be deleted", result.getContacts().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStatelessUpdateDeleteChildrenForNonBeanCollection() {
|
||||
|
||||
// arrange
|
||||
Contact contact = new Contact();
|
||||
contact.setFirstName("wobu :P");
|
||||
|
||||
Customer customer = new Customer();
|
||||
customer.setName("something");
|
||||
customer.setContacts(new ArrayList<>());
|
||||
customer.getContacts().add(contact);
|
||||
|
||||
server.save(customer);
|
||||
|
||||
// act
|
||||
Customer customerWithChange = new Customer();
|
||||
customerWithChange.setId(customer.getId());
|
||||
customerWithChange.setName("new name");
|
||||
|
||||
// with Ebean enhancement this loads the an empty contacts BeanList
|
||||
customerWithChange.setContacts(Collections.<Contact>emptyList());
|
||||
|
||||
Assert.assertTrue(containsContacts(customerWithChange));
|
||||
server.update(customerWithChange);
|
||||
|
||||
Customer result = Ebean.find(Customer.class, customer.getId());
|
||||
|
||||
// assert empty bean list was ignore (missing children not deleted)
|
||||
Assert.assertNotNull(result.getContacts());
|
||||
Assert.assertTrue("the contacts were deleted", result.getContacts().isEmpty());
|
||||
}
|
||||
|
||||
private boolean containsContacts(Customer cust) {
|
||||
return server.getBeanState(cust).getLoadedProps().contains("contacts");
|
||||
}
|
||||
|
||||
/**
|
||||
* when using stateless updates with recursive calls,
|
||||
* the version column shouldn't decide to use insert instead of update,
|
||||
* although an ID has been set.
|
||||
*/
|
||||
@Test
|
||||
public void testStatelessRecursiveUpdateWithVersionField() {
|
||||
// arrange
|
||||
Contact contact1 = new Contact();
|
||||
contact1.setLastName("contact1");
|
||||
|
||||
Contact contact2 = new Contact();
|
||||
contact2.setLastName("contact2");
|
||||
|
||||
Customer customer = new Customer();
|
||||
customer.setName("something");
|
||||
customer.getContacts().add(contact1);
|
||||
customer.getContacts().add(contact2);
|
||||
|
||||
server.save(customer);
|
||||
|
||||
// act
|
||||
Contact updateContact1 = new Contact();
|
||||
updateContact1.setId(contact1.getId());
|
||||
|
||||
Contact updateContact2 = new Contact();
|
||||
updateContact2.setId(contact2.getId());
|
||||
|
||||
Customer updateCustomer = new Customer();
|
||||
updateCustomer.setId(customer.getId());
|
||||
updateCustomer.getContacts().add(updateContact1);
|
||||
updateCustomer.getContacts().add(updateContact2);
|
||||
|
||||
server.update(updateCustomer);
|
||||
|
||||
// assert
|
||||
// maybe check if update instead of insert has been executed,
|
||||
// currently "Unique index or primary key violation" PersistenceException is throwing
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStatelessRecursiveUpdateWithChangesInDetailOnly() {
|
||||
// arrange
|
||||
Contact contact1 = new Contact();
|
||||
contact1.setLastName("contact1");
|
||||
|
||||
Contact contact2 = new Contact();
|
||||
contact2.setLastName("contact2");
|
||||
|
||||
Customer customer = new Customer();
|
||||
customer.setName("something");
|
||||
customer.getContacts().add(contact1);
|
||||
customer.getContacts().add(contact2);
|
||||
|
||||
server.save(customer);
|
||||
|
||||
|
||||
// act
|
||||
Contact updateContact1 = new Contact();
|
||||
updateContact1.setId(contact1.getId());
|
||||
updateContact1.setLastName("contact1-changed");
|
||||
|
||||
|
||||
Contact updateContact3 = new Contact();
|
||||
//updateContact3.setId(contact3.getId());
|
||||
updateContact3.setLastName("contact3-added");
|
||||
|
||||
Customer updateCustomer = new Customer();
|
||||
updateCustomer.setId(customer.getId());
|
||||
updateCustomer.getContacts().add(updateContact1);
|
||||
updateCustomer.getContacts().add(updateContact3);
|
||||
|
||||
// not adding contact2 so it will get deleted
|
||||
//updateCustomer.getContacts().add(updateContact2);
|
||||
|
||||
server.update(updateCustomer);
|
||||
|
||||
|
||||
// assert
|
||||
Customer assCustomer = server.find(Customer.class, customer.getId());
|
||||
List<Contact> assContacts = assCustomer.getContacts();
|
||||
assertEquals(2, assContacts.size());
|
||||
Set<Integer> ids = new LinkedHashSet<>();
|
||||
Set<String> names = new LinkedHashSet<>();
|
||||
for (Contact contact : assContacts) {
|
||||
ids.add(contact.getId());
|
||||
names.add(contact.getLastName());
|
||||
}
|
||||
Assert.assertTrue(ids.contains(contact1.getId()));
|
||||
Assert.assertTrue(ids.contains(updateContact3.getId()));
|
||||
Assert.assertFalse(ids.contains(contact2.getId()));
|
||||
|
||||
Assert.assertTrue(names.contains(updateContact1.getLastName()));
|
||||
Assert.assertTrue(names.contains(updateContact3.getLastName()));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testStatelessRecursiveUpdateWithChangesInDetailOnlyAnd() {
|
||||
// arrange
|
||||
Contact contact1 = new Contact();
|
||||
contact1.setLastName("contact1");
|
||||
|
||||
Contact contact2 = new Contact();
|
||||
contact2.setLastName("contact2");
|
||||
|
||||
Customer customer = new Customer();
|
||||
customer.setName("something");
|
||||
customer.getContacts().add(contact1);
|
||||
customer.getContacts().add(contact2);
|
||||
|
||||
server.save(customer);
|
||||
|
||||
|
||||
// act
|
||||
Contact updateContact1 = new Contact();
|
||||
updateContact1.setId(contact1.getId());
|
||||
updateContact1.setLastName("contact1-changed");
|
||||
|
||||
|
||||
Contact updateContact3 = new Contact();
|
||||
updateContact3.setLastName("contact3-added");
|
||||
|
||||
Customer updateCustomer = new Customer();
|
||||
updateCustomer.setId(customer.getId());
|
||||
updateCustomer.getContacts().add(updateContact1);
|
||||
updateCustomer.getContacts().add(updateContact3);
|
||||
|
||||
// not adding contact2 but it won't be deleted in this case
|
||||
boolean deleteMissingChildren = false;
|
||||
server.update(updateCustomer, null, deleteMissingChildren);
|
||||
|
||||
|
||||
// assert
|
||||
Customer assCustomer = server.find(Customer.class, customer.getId());
|
||||
List<Contact> assContacts = assCustomer.getContacts();
|
||||
|
||||
// contact 2 was not deleted this time
|
||||
assertEquals(3, assContacts.size());
|
||||
|
||||
Set<Integer> ids = new LinkedHashSet<>();
|
||||
Set<String> names = new LinkedHashSet<>();
|
||||
for (Contact contact : assContacts) {
|
||||
ids.add(contact.getId());
|
||||
names.add(contact.getLastName());
|
||||
}
|
||||
Assert.assertTrue(ids.contains(contact1.getId()));
|
||||
Assert.assertTrue(ids.contains(updateContact3.getId()));
|
||||
Assert.assertTrue(ids.contains(contact2.getId()));
|
||||
|
||||
Assert.assertTrue(names.contains(updateContact1.getLastName()));
|
||||
Assert.assertTrue(names.contains(contact2.getLastName()));
|
||||
Assert.assertTrue(names.contains(updateContact3.getLastName()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package org.tests.update;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.EbeanServer;
|
||||
import io.ebean.Transaction;
|
||||
import org.tests.model.basic.EBasicVer;
|
||||
import org.avaje.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class TestUpdateAllLoadedProperties extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
EBasicVer basic1 = new EBasicVer("basic1");
|
||||
basic1.setDescription("aaa");
|
||||
Ebean.save(basic1);
|
||||
|
||||
EBasicVer basic2 = new EBasicVer("basic2");
|
||||
basic1.setDescription("bbb");
|
||||
Ebean.save(basic2);
|
||||
|
||||
|
||||
EbeanServer server = Ebean.getDefaultServer();
|
||||
Transaction txn = server.beginTransaction();
|
||||
try {
|
||||
|
||||
txn.setUpdateAllLoadedProperties(true);
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
basic2.setDescription("bbb-mod");
|
||||
server.save(basic2, txn);
|
||||
|
||||
basic1.setName("basic1-mod");
|
||||
basic1.setDescription("aaa-mod");
|
||||
server.save(basic1, txn);
|
||||
|
||||
txn.commit();
|
||||
|
||||
List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
|
||||
assertEquals(2, loggedSql.size());
|
||||
// all properties in the bean
|
||||
assertTrue(loggedSql.get(0), loggedSql.get(0).contains("update e_basicver set name=?, description=?, other=?, last_update=? where id=? and last_update=?; --bind("));
|
||||
assertTrue(loggedSql.get(1), loggedSql.get(1).contains("update e_basicver set name=?, description=?, other=?, last_update=? where id=? and last_update=?; --bind("));
|
||||
|
||||
} finally {
|
||||
txn.end();
|
||||
}
|
||||
|
||||
testPartiallyLoaded(basic1.getId(), basic2.getId());
|
||||
|
||||
}
|
||||
|
||||
private void testPartiallyLoaded(Integer id1, Integer id2) {
|
||||
|
||||
List<Integer> ids = new ArrayList();
|
||||
ids.add(id1);
|
||||
ids.add(id2);
|
||||
|
||||
List<EBasicVer> beans = Ebean.find(EBasicVer.class)
|
||||
.select("name, other")
|
||||
.where().idIn(ids)
|
||||
.order().asc("id")
|
||||
.findList();
|
||||
|
||||
assertEquals(2, beans.size());
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
|
||||
Transaction txn = Ebean.beginTransaction();
|
||||
try {
|
||||
txn.setUpdateAllLoadedProperties(true);
|
||||
|
||||
EBasicVer basic1 = beans.get(0);
|
||||
basic1.setName("jim");
|
||||
Ebean.save(basic1);
|
||||
|
||||
EBasicVer basic2 = beans.get(1);
|
||||
basic2.setName("john");
|
||||
basic2.setOther("otherDesc");
|
||||
Ebean.save(basic2);
|
||||
|
||||
txn.commit();
|
||||
|
||||
} finally {
|
||||
txn.end();
|
||||
}
|
||||
|
||||
List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
|
||||
assertThat(loggedSql).hasSize(2);
|
||||
assertThat(loggedSql.get(0)).contains("update e_basicver set name=?, other=?, last_update=? where id=?; --bind(");
|
||||
assertThat(loggedSql.get(1)).contains("update e_basicver set name=?, other=?, last_update=? where id=?; --bind(");
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package org.tests.update;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.SqlQuery;
|
||||
import io.ebean.SqlRow;
|
||||
import org.tests.model.basic.Customer;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class TestUpdatePartial extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
Customer c = new Customer();
|
||||
c.setName("TestUpdateMe");
|
||||
c.setStatus(Customer.Status.ACTIVE);
|
||||
c.setSmallnote("a note");
|
||||
|
||||
Ebean.save(c);
|
||||
checkDbStatusValue(c.getId(), "A");
|
||||
|
||||
Customer c2 = Ebean.find(Customer.class)
|
||||
.setUseCache(false)
|
||||
.select("status, smallnote")
|
||||
.setId(c.getId())
|
||||
.findUnique();
|
||||
|
||||
c2.setStatus(Customer.Status.INACTIVE);
|
||||
c2.setSmallnote("2nd note");
|
||||
|
||||
Ebean.save(c2);
|
||||
checkDbStatusValue(c.getId(), "I");
|
||||
|
||||
Customer c3 = Ebean.find(Customer.class)
|
||||
.setUseCache(false)
|
||||
.select("status")
|
||||
.setId(c.getId())
|
||||
.findUnique();
|
||||
|
||||
c3.setStatus(Customer.Status.NEW);
|
||||
c3.setSmallnote("3rd note");
|
||||
|
||||
Ebean.save(c3);
|
||||
checkDbStatusValue(c.getId(), "N");
|
||||
}
|
||||
|
||||
private void checkDbStatusValue(Integer custId, String dbStatus) {
|
||||
SqlQuery sqlQuery = Ebean.createSqlQuery("select id, status from o_customer where id = ?");
|
||||
sqlQuery.setParameter(1, custId);
|
||||
SqlRow sqlRow = sqlQuery.findUnique();
|
||||
String status = sqlRow.getString("status");
|
||||
assertEquals(dbStatus, status);
|
||||
}
|
||||
|
||||
/**
|
||||
* If we have no changes detected, don't execute an Update and don't update the Version column.
|
||||
*/
|
||||
@Test
|
||||
public void testWithoutChangesAndVersionColumn() {
|
||||
|
||||
Customer customer = new Customer();
|
||||
customer.setName("something");
|
||||
|
||||
Ebean.save(customer);
|
||||
|
||||
Customer customerWithoutChanges = Ebean.find(Customer.class, customer.getId());
|
||||
Ebean.save(customerWithoutChanges);
|
||||
|
||||
assertEquals(customer.getUpdtime().getTime(), customerWithoutChanges.getUpdtime().getTime());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package org.tests.update;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import org.tests.model.basic.EBasic;
|
||||
import org.tests.model.basic.EBasic.Status;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestUpdatePartialNoVer extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
EBasic b = new EBasic();
|
||||
b.setName("testpart");
|
||||
b.setStatus(Status.ACTIVE);
|
||||
b.setDescription("description");
|
||||
|
||||
Ebean.save(b);
|
||||
|
||||
EBasic basic = Ebean.find(EBasic.class).select("status, name").setId(b.getId()).findUnique();
|
||||
|
||||
basic.setName("modiName");
|
||||
|
||||
Ebean.save(basic);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user