I have defined two entities and the respective two request types in a derived RequestFactory. When I persist a single entity in a unit test it works fine. When I try to persist both entities within the same unit test one after the other, the first entity is stored and the second fire also ends up in onSuccess, but no data is stored in the database. I use one RequestFacory, one EventBus and two request types.
When I place the second persist.fire into the first onSuccess method the corresponding onSuccess method is not hit and the tests terminates without any message.
How to persist two unrelated entities in one code place?
//Company
@Entity
public class Company {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "version")
private Integer version;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
private String instance;
public static Company findCompany(Long id) {
EntityManager em = emf.createEntityManager();
try {
Company company = em.find(Company.class, id);
return company;
} finally {
em.close();
}
}
public void persist() throws PersistenceException {
EntityManager em = emf.createEntityManager();
EntityTransaction tx = null;
try {
tx = em.getTransaction();
tx.begin();
em.persist(this);
tx.commit();
} catch (RuntimeException e) {
tx.rollback();
throw e;
} finally {
em.close();
}
}
}
//CompanyRequest
@Service(Company.class)
public interface CompanyRequest extends RequestContext {
Request<CompanyProxy> findCompany(Long id);
InstanceRequest<CompanyProxy, Void> persist();
InstanceRequest<CompanyProxy, Void> remove();
}
//ServiceProvider
similar to Company with ServiceProviderRequest
//RequestFactory
public interface AftdRequestFactory extends RequestFactory {
CompanyRequest companyRequest();
ServiceProviderRequest serviceProviderRequest();
}
//UnitTest
public void testSetupDatabase() {
final EventBus eventBus = new SimpleEventBus();
final AftdRequestFactory requestFactory = GWT.create(AftdRequestFactory.class);
requestFactory.initialize(eventBus);
CompanyRequest request = requestFactory.companyRequest();
CompanyProxy newCompany = request.create(CompanyProxy.class);
newCompany.setInstance("1");
request.persist().using(newCompany);
request.fire(new Receiver<Void>() {
@Override
public void onSuccess(Void arg0) {
ServiceProviderRequest spRequest = requestFactory.serviceProviderRequest();
ServiceProviderProxy newServiceProvider = spRequest.create(ServiceProviderProxy.class);
newServiceProvider.setInstance("12345");
spRequest.persist().using(newServiceProvider);
spRequest.fire(new Receiver<Void>() {
@Override
public void onSuccess(Void arg0) {
System.out.println("service provider instance created");
}
@Override
public void onFailure(ServerFailure error) {
}
});
}
@Override
public void onFailure(ServerFailure error) {
System.out.println("could not create company instances");
}
});
}