我在派生的RequestFactory中定义了两个实体和相应的两个请求类型。 当我在单元测试中坚持单个实体时,它可以正常工作。当我尝试将两个实体一个接一个地保存在同一个单元测试中时,第一个实体被存储,第二个实体也在onSuccess中结束,但数据库中没有存储数据。 我使用一个RequestFacory,一个EventBus和两个请求类型。
当我将第二个persist.fire放入第一个onSuccess方法时,没有命中相应的onSuccess方法,测试终止,没有任何消息。
如何在一个代码位置保留两个不相关的实体?
//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");
}
});
}
答案 0 :(得分:0)
根本问题似乎是一个时间问题。当我将'delayTestFinish(30000)'添加到gwt单元测试的开头时,主线程等待足够长的时间,直到调用onSuccess()方法并完成异步线程。