CUBA 平台使用错误Relationship that was not marked cascade PERSIST

最近练习公司的CUBA平台,在持久化实例时出现了一个错误,

During synchronization a new object was found through a relationship that was not marked cascade PERSIST

网上很多人都再说添加

cascade PERSIST 就可以了,但对于我来说没有任何作用,最后在cuba的官网上找到了解决方法

This exception occurs when you save (persist or merge) an object with a reference to another object which is new (does not exists in the database). To avoid the error, such new object should be saved in the same persistence context, i.e. in the same transaction.

For example, you have two related entities: Customer and Order, and Order can contain a reference to Customer. Then, you have an editor screen for Order with the corresponding datasource. Imagine that when you edit a new Order, you programmatically create a new instance of Customer and set it to Order’s attribute. After that, you commit the screen. DataManager on middleware receives a CommitContext with the new Order which has the new Customer as a reference. It invokes EntityManager.persist(order), commits the transaction, and here the exception occurs - the Customer is not passed to the persistence context and there is no CASCADE relationship between Order and Customer.

The solution is to pass the new Customer together with the new Order. It can be done automatically if the screen contains a datasource for Customer - all datasources are saved on screen commit. Alternatively, the new Customer instance can be added to CommitContext in a DsContext.BeforeCommitListener42.

其实很简单,就是说你要持久化的这个entity里的有一个或多个成员类,你需要在持久化这个entity之前将成员类先持久化,并且实在一个事物中,

比如

EntityManager.persist(order);

EntityManager.persist(Customer.);

本人懂的东西不多,看不到本质,希望看到的人将自己见解告知,共同进步

猜你喜欢

转载自blog.csdn.net/whiteautumnlovers/article/details/79279633