我在圣杯中得到了违反诚信例外。如何在圣杯中删除相关实体?
我有一个简单的电子商务Grails 5应用程序,其中我将Cartitem定义为:
class CartItem {
Customer customer
Product product
Integer quantity}
我有产品类别:
class Product {
String name
Double price }
删除产品时,也必须删除具有此产品(如果有)的Cartitem。 (这也适用于客户)。 现在,当我删除产品时,如果此产品有Cartitem,我会遇到违反诚信例外。
我正在使用H2内存数据库使用Hibernate。在不使用Hibernate配置的情况下(除非这是唯一的方法)如何实现删除级联的级联以自动删除使用Grails Gorm的关联Cartitem删除行为?
当客户将产品添加到购物车中时,将创建Cartitem实例。
当删除Cartitem(从购物车中删除产品时)时,相关产品或客户什么都不会发生。但是,当删除产品时,还必须删除其相关的Cartitem。
I have a simple eCommerce grails 5 application in which I define CartItem as:
class CartItem {
Customer customer
Product product
Integer quantity}
I have Product class:
class Product {
String name
Double price }
When a product is deleted, cartItem having this product(if any) must also be deleted. (The same also applies to Customer).
Right now when I delete product, I get integrity violation exception if there is cartItem with this product.
I am using Hibernate with H2 in-memory database. Without using hibernate configuration (unless this is the only way) how can I achieve product delete cascade to automatic deletion of associate cartItem delete behaviour using grails GORM?
An instance of CartItem is created when a customer adds product to shopping cart.
When a cartItem is deleted (when a product is removed from the shopping cart) nothing happens to the related product or customer. But when a product is deleted its associated cartItem must also be deleted.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Cartitem不属于产品。因此,当产品被删除时,圣杯无法将其删除。但是您可以利用Grails域类事件 - “ BeforeDelete”。
https://docs.grails.grails.grails.grails.org/s.org/3.0.x/guide/ gorm.html#事件autotimestamping
CartItem doesn't belong to Product. And therefore Grails cannot delete it when the Product is deleted. But you can take advantage of grails domain class events - "beforeDelete".
https://docs.grails.org/3.0.x/guide/GORM.html#eventsAutoTimestamping
您不得在此配置中删除产品。这是Grails文件。
https:///docs.grails.orgs.org/5.0。 0-rc4/ref/database%20映射/cascade.html
该文档以Hasmany参考为示例。
在我看来,您还需要在Cartitem类中拥有Hasmany,以便在Grails文档中查看Exmaple。
您可以尝试将此行添加到Cartitem类中,以查看是否有任何错误,说它找不到产品参考。
我以前从未尝试过。所以我不能给你一个直接的答案。
如果这不起作用,则必须自己滚动。您还可以从数据库中更改FK索引以启用Cascade Delete。确保您记录了自己的工作。否则,下一个接管该项目的人将抱怨。
You may not delete Product in this configuration. This is the grails document.
https://docs.grails.org/5.0.0-RC4/ref/Database%20Mapping/cascade.html
The document shows the example with a hasMany reference.
It looks to me that you also need to have hasMany in your CartItem class for this to work looking at the exmaple from the grails document.
You can try to add this line to your CartItem class to see if there is any error saying it cannot find the product reference.
I have not tried this before. So I cannot give you a straight answer.
If this does not work, you will have to roll your own. You can also make changes to the FK index from the database to enable cascade delete. Make sure you document what you did. Otherwise the next guy who picked up the project will be complaining.