【发布时间】:2011-05-12 22:05:41
【问题描述】:
我们有一个在 Glassfish 3.1 中运行的 Java EE 应用程序,我们的 JPA 模型(使用 EclipseLink)组织如下:
Customer
-> String firstName
-> String lastName
-> Address adress
-> List<Attribute> attributes
-> Int age
Address
-> String street
-> Int zip
-> String city
Attribute
-> String code
-> String name
firstName 和lastName 等大多数属性都使用@Column(nullable=false) 进行注释。现在我们这样做:
@Stateless
public class CustomerController {
@PersistenceContext(unitName = "CustomerService")
private EntityManager em;
@EJB
private AttributeController attributeController;
public String createCustomer() {
Customer customer = new Customer();
customer.firstName = "Hans";
customer.lastName = "Peter";
customer.address = new Address();
customer.adress.street = ...
customer.attributes = new ArrayList<Attribute>();
customer.attributes.add(attributeController.getByCode("B"));
customer.attributes.add(attributeController.getByCode("T"));
customer.age = 27;
em.persist(customer);
}
}
这适用于像上面这样的小类,但我们现在引入了更多与客户相关的对象,例如带有 @OneToMany 的属性,并从其他 @EJBs 加载,例如 attributeController。
对于“大”模型,现在似乎在加载相关对象的过程中提交了一个事务,因为我们得到了一个ERROR: null value in column "age" violates not-null constraint。由于我们使用 JTA 容器管理事务并且没有将 @TransactionAttribute 设置为默认 REQUIRED 以外的其他值,因此我们不直接控制事务并且不知道这里出了什么问题。
在提交之前是否有一定数量的“工作单元”可以公开?我们是否加载了错误的相关对象?我们是否犯了其他重大错误?
只要我们省略了nullable=false 约束,一切正常...
【问题讨论】:
标签: jpa transactions ejb constraints jta