mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Add test for oneToOne cascade update
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
package com.avaje.tests.iud;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.tests.model.info.InfoCompany;
|
||||
import com.avaje.tests.model.info.InfoCustomer;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestInfoOneToOne extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test_cascade_oneToOne() {
|
||||
|
||||
InfoCompany company = new InfoCompany();
|
||||
company.setName("info company");
|
||||
|
||||
InfoCustomer customer = new InfoCustomer();
|
||||
customer.setName("first info cust");
|
||||
customer.setInfos(company);
|
||||
|
||||
customer.save();
|
||||
|
||||
// assert both are inserted
|
||||
Assert.assertNotNull(customer.getId());
|
||||
Assert.assertNotNull(company.getId());
|
||||
|
||||
// both can be fetched
|
||||
Assert.assertNotNull(InfoCustomer.find.byId(customer.getId()));
|
||||
Assert.assertNotNull(InfoCompany.find.byId(company.getId()));
|
||||
|
||||
|
||||
// just update the customer
|
||||
customer.setName("first mod");
|
||||
customer.update();
|
||||
|
||||
// update the customer and company
|
||||
customer.getInfos().setName("2nd mod company");
|
||||
customer.setName("2nd mod customer");
|
||||
customer.update();
|
||||
|
||||
// fetch and then update both
|
||||
InfoCustomer fetchedCustomer = InfoCustomer.find.byId(customer.getId());
|
||||
fetchedCustomer.getInfos().setName("3rd mod company");
|
||||
fetchedCustomer.setName("3rd mod customer");
|
||||
fetchedCustomer.update();
|
||||
|
||||
|
||||
// delete both customer and company
|
||||
fetchedCustomer.delete();
|
||||
Assert.assertNull(InfoCustomer.find.byId(customer.getId()));
|
||||
Assert.assertNull(InfoCompany.find.byId(company.getId()));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.avaje.tests.model.info;
|
||||
|
||||
import com.avaje.ebean.Model;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class InfoCompany extends Model {
|
||||
|
||||
public static Finder<Long,InfoCompany> find = new Finder<Long,InfoCompany>(Long.class, InfoCompany.class);
|
||||
|
||||
@Id
|
||||
Long id;
|
||||
|
||||
@Version
|
||||
Long version;
|
||||
|
||||
String name;
|
||||
|
||||
@OneToMany(mappedBy = "company", cascade = CascadeType.ALL)
|
||||
List<InfoContact> contacts = new ArrayList<InfoContact>();
|
||||
|
||||
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 String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<InfoContact> getContacts() {
|
||||
return contacts;
|
||||
}
|
||||
|
||||
public void setContacts(List<InfoContact> contacts) {
|
||||
this.contacts = contacts;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.avaje.tests.model.info;
|
||||
|
||||
import com.avaje.ebean.Model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Version;
|
||||
|
||||
@Entity
|
||||
public class InfoContact extends Model {
|
||||
|
||||
@Id
|
||||
Long id;
|
||||
|
||||
@Version
|
||||
Long version;
|
||||
|
||||
String name;
|
||||
|
||||
@ManyToOne(optional = false)
|
||||
InfoCompany company;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(Long version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public InfoCompany getCompany() {
|
||||
return company;
|
||||
}
|
||||
|
||||
public void setCompany(InfoCompany company) {
|
||||
this.company = company;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.avaje.tests.model.info;
|
||||
|
||||
import com.avaje.ebean.Model;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
public class InfoCustomer extends Model {
|
||||
|
||||
public static Finder<Long,InfoCustomer> find = new Finder<Long,InfoCustomer>(Long.class, InfoCustomer.class);
|
||||
|
||||
@Id
|
||||
Long id;
|
||||
|
||||
@Version
|
||||
Long version;
|
||||
|
||||
String name;
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
//@JoinColumn(name = "id_info_company")
|
||||
InfoCompany infos = new InfoCompany();
|
||||
|
||||
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 String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public InfoCompany getInfos() {
|
||||
return infos;
|
||||
}
|
||||
|
||||
public void setInfos(InfoCompany infos) {
|
||||
this.infos = infos;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user