1

我在派生的 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");
        }
    });
}
4

1 回答 1

0

根本问题似乎是时间问题。当我在 gwt 单元测试的开头添加“delayTestFinish(30000)”时,主线程会等待足够长的时间,直到两个 onSuccess() 方法都被调用并且异步线程完成。

于 2013-01-28T14:26:42.210 回答