首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >级联是否发生在一个事务中?

级联是否发生在一个事务中?
EN

Stack Overflow用户
提问于 2020-11-10 06:12:56
回答 1查看 97关注 0票数 1

我保存了级联持久化productMaterial的产品。但是,当productMaterial抛出DataIntegrityViolationException时,产品是回滚的,这似乎是在一个事务中完成的,但我没有发现任何文档表明它是这样做的。有人能帮我澄清一下吗?

注:I使用@Transactional

代码语言:javascript
复制
Material material = new Material();
material.setId(1);

Product newProduct = new Product();
ProductMaterial productMaterial = new ProductMaterial();

newProduct.setName("bàn chải");
newProduct.setPrice(1000);
newProduct.setCreatedAt(new Date());
newProduct.setProductMaterials(Collections.singletonList(productMaterial));

productMaterial.setProduct(newProduct);
productMaterial.setMaterial(material);

productRepository.save(newProduct);

以下是hibernate的执行:

代码语言:javascript
复制
Hibernate: 
    /* insert com.vietnam.hanghandmade.entities.Product
        */ insert 
        into
            product
            (created_at, name, price, id) 
        values
            (?, ?, ?, ?)
2020-11-10 14:55:38.281 TRACE 65729 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [TIMESTAMP] - [Tue Nov 10 14:55:38 JST 2020]
2020-11-10 14:55:38.281 TRACE 65729 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [VARCHAR] - [bàn chải]
2020-11-10 14:55:38.281 TRACE 65729 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [INTEGER] - [1000]
2020-11-10 14:55:38.281 TRACE 65729 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder      : binding parameter [4] as [OTHER] - [e5729490-a0f8-48e7-9600-eeeba8b8f279]
Hibernate: 
    /* insert com.vietnam.hanghandmade.entities.ProductMaterial
        */ insert 
        into
            product_material
            (material_id, product_id) 
        values
            (?, ?)
2020-11-10 14:55:38.324 TRACE 65729 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [INTEGER] - [1]
2020-11-10 14:55:38.324 TRACE 65729 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [OTHER] - [e5729490-a0f8-48e7-9600-eeeba8b8f279]
2020-11-10 14:55:38.328  WARN 65729 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 23503
2020-11-10 14:55:38.328 ERROR 65729 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: insert or update on table "product_material" violates foreign key constraint "product_material_material_id_fkey"
  Detail: Key (material_id)=(1) is not present in table "material".
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-10 06:58:55

注意:这个答案忽略了问题的要点,这个问题是关于“级联持久化”的--它谈到外键的“级联删除”。

级联删除或更新是实现外键约束的系统触发器操作的一部分,因此它运行在与触发语句相同的事务中。

我在精细的手册中找不到详细说明这一点的位置,但是如果您仔细考虑一下,很明显:如果级联删除是在单独的事务中运行的,那么删除就有可能成功,级联删除可能会失败,这将使数据库不一致,因此无法选择。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64763751

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档