mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Fix for #57 - Embedded Entities with Autofetch Exception, plus some additional tests
This commit is contained in:
@@ -1,14 +1,40 @@
|
||||
package com.avaje.tests.autofetch;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.embedded.EMain;
|
||||
import org.junit.Test;
|
||||
import java.util.List;
|
||||
import com.avaje.tests.model.embedded.Eembeddable;
|
||||
|
||||
|
||||
public class AutofetchEmbeddedTest extends BaseTestCase {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(AutofetchEmbeddedTest.class);
|
||||
|
||||
@Test
|
||||
public void testEmbeddedBeanLazyLoadAndUpdate() {
|
||||
|
||||
EMain testBean = new EMain();
|
||||
testBean.setName("test");
|
||||
testBean.getEmbeddable().setDescription("test description");
|
||||
Ebean.save(testBean);
|
||||
|
||||
EMain partialBean = Ebean.find(EMain.class).select("version").setId(testBean.getId()).findUnique();
|
||||
|
||||
logger.info(" -- invoke lazy loading of embedded bean");
|
||||
Eembeddable embeddable = partialBean.getEmbeddable();
|
||||
embeddable.setDescription("modified description");
|
||||
|
||||
logger.info(" -- update bean");
|
||||
Ebean.save(partialBean);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmbeddedBeanQueryTuning() {
|
||||
Ebean.getServer(null).getAdminAutofetch().setProfiling(true);
|
||||
|
||||
@@ -7,6 +7,7 @@ import java.util.List;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.EbeanServer;
|
||||
import com.avaje.ebean.TxRunnable;
|
||||
import com.avaje.tests.model.basic.Order.Status;
|
||||
|
||||
public class ResetBasicData {
|
||||
|
||||
@@ -273,6 +274,7 @@ public class ResetBasicData {
|
||||
Product product1 = Ebean.getReference(Product.class, 1);
|
||||
|
||||
Order order = new Order();
|
||||
order.setStatus(Status.SHIPPED);
|
||||
order.setCustomer(customer);
|
||||
|
||||
List<OrderDetail> details = new ArrayList<OrderDetail>();
|
||||
@@ -290,6 +292,7 @@ public class ResetBasicData {
|
||||
Product product3 = Ebean.getReference(Product.class, 3);
|
||||
|
||||
Order order = new Order();
|
||||
order.setStatus(Status.COMPLETE);
|
||||
order.setCustomer(customer);
|
||||
|
||||
List<OrderDetail> details = new ArrayList<OrderDetail>();
|
||||
@@ -301,15 +304,14 @@ public class ResetBasicData {
|
||||
|
||||
Ebean.save(order);
|
||||
}
|
||||
|
||||
private void createOrder4(Customer customer) {
|
||||
|
||||
private void createOrder4(Customer customer) {
|
||||
|
||||
Order order = new Order();
|
||||
order.setCustomer(customer);
|
||||
Order order = new Order();
|
||||
order.setCustomer(customer);
|
||||
|
||||
order.addShipment(new OrderShipment());
|
||||
order.addShipment(new OrderShipment());
|
||||
|
||||
Ebean.save(order);
|
||||
}
|
||||
Ebean.save(order);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
package com.avaje.tests.model.embedded;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import javax.persistence.AttributeOverride;
|
||||
import javax.persistence.AttributeOverrides;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Embedded;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Version;
|
||||
|
||||
@Entity
|
||||
public class EInvoice {
|
||||
|
||||
public enum State {
|
||||
New, Processing, Approved
|
||||
}
|
||||
|
||||
@Id
|
||||
Long id;
|
||||
|
||||
@Version
|
||||
Long version;
|
||||
|
||||
Date date;
|
||||
|
||||
State state;
|
||||
|
||||
@ManyToOne
|
||||
EPerson person;
|
||||
|
||||
@Embedded
|
||||
@AttributeOverrides({
|
||||
@AttributeOverride(name = "street", column = @Column(name = "ship_street")),
|
||||
@AttributeOverride(name = "suburb", column = @Column(name = "ship_suburb")),
|
||||
@AttributeOverride(name = "city", column = @Column(name = "ship_city"))
|
||||
})
|
||||
EAddress shipAddress;
|
||||
|
||||
@Embedded
|
||||
EAddress billAddress;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(Long version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public State getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(State state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public Date getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(Date date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public EPerson getPerson() {
|
||||
return person;
|
||||
}
|
||||
|
||||
public void setPerson(EPerson person) {
|
||||
this.person = person;
|
||||
}
|
||||
|
||||
public EAddress getShipAddress() {
|
||||
return shipAddress;
|
||||
}
|
||||
|
||||
public void setShipAddress(EAddress shipAddress) {
|
||||
this.shipAddress = shipAddress;
|
||||
}
|
||||
|
||||
public EAddress getBillAddress() {
|
||||
return billAddress;
|
||||
}
|
||||
|
||||
public void setBillAddress(EAddress billAddress) {
|
||||
this.billAddress = billAddress;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.avaje.tests.query.embedded;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.embedded.EAddress;
|
||||
import com.avaje.tests.model.embedded.EInvoice;
|
||||
import com.avaje.tests.model.embedded.EInvoice.State;
|
||||
|
||||
public class TestMultipleEmbeddedLoading extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testSimpleCase() {
|
||||
|
||||
// prepare test
|
||||
EAddress ship = new EAddress();
|
||||
ship.setStreet("1 Banana St");
|
||||
ship.setSuburb("Suburb");
|
||||
ship.setCity("Auckland");
|
||||
|
||||
EAddress bill = new EAddress();
|
||||
bill.setStreet("2 Apple St");
|
||||
bill.setSuburb("Suburb");
|
||||
bill.setCity("Auckland");
|
||||
|
||||
EInvoice invoice = new EInvoice();
|
||||
invoice.setDate(new Date(System.currentTimeMillis()));
|
||||
invoice.setState(State.New);
|
||||
invoice.setShipAddress(ship);
|
||||
invoice.setBillAddress(bill);
|
||||
|
||||
// act: save and fetch
|
||||
Ebean.save(invoice);
|
||||
|
||||
EInvoice invoice2 = Ebean.find(EInvoice.class)
|
||||
.where().idEq(invoice.getId())
|
||||
.findUnique();
|
||||
|
||||
// assert fetched bean populated as expected
|
||||
Assert.assertEquals(invoice.getId(), invoice2.getId());
|
||||
Assert.assertEquals(invoice.getState(), invoice2.getState());
|
||||
Assert.assertEquals(invoice.getDate(), invoice2.getDate());
|
||||
Assert.assertEquals("2 Apple St", invoice.getBillAddress().getStreet());
|
||||
Assert.assertEquals("2 Apple St", invoice2.getBillAddress().getStreet());
|
||||
|
||||
// act: only update one of the embedded fields
|
||||
invoice2.getBillAddress().setStreet("3 Pineapple St");
|
||||
// bean should be dirty
|
||||
Ebean.save(invoice2);
|
||||
|
||||
EInvoice invoice3 = Ebean.find(EInvoice.class)
|
||||
.where().idEq(invoice.getId())
|
||||
.findUnique();
|
||||
|
||||
// assert field has updated value
|
||||
Assert.assertEquals("3 Pineapple St", invoice3.getBillAddress().getStreet());
|
||||
|
||||
|
||||
// fetch a partial
|
||||
EInvoice invoicePartial = Ebean.find(EInvoice.class)
|
||||
.select("state, date")
|
||||
.where().idEq(invoice.getId())
|
||||
.findUnique();
|
||||
|
||||
// lazy load of embedded bean
|
||||
EAddress billAddress = invoicePartial.getBillAddress();
|
||||
|
||||
Assert.assertNotNull(billAddress);
|
||||
Assert.assertEquals("3 Pineapple St", billAddress.getStreet());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package com.avaje.tests.query.other;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.BeanState;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.Order.Status;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestFindPartialWithConstructorSetFields extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
|
||||
List<Order> list = Ebean.find(Order.class)
|
||||
.select("shipDate")
|
||||
.findList();
|
||||
|
||||
for (Order order : list) {
|
||||
BeanState beanState = Ebean.getBeanState(order);
|
||||
Set<String> loadedProps = beanState.getLoadedProps();
|
||||
Assert.assertTrue(loadedProps.contains("shipDate"));
|
||||
Assert.assertTrue(!loadedProps.contains("status"));
|
||||
Assert.assertTrue(!loadedProps.contains("orderDate"));
|
||||
|
||||
Status status = order.getStatus();
|
||||
Date orderDate = order.getOrderDate();
|
||||
System.out.println("-- order - "+order.getId()+" status:"+status+" date:"+orderDate);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user