jpa manytoone cascade MERGE和PERSIST

jpa manytoone cascade MERGE和PERSIST

    • merge
    • persist

merge

  • 当不传入id和外键id时

{“title”:"",“article”:"## markdown text"}

数据库插入一条没有外键的数据
sql

Hibernate: select next_val as id_val from hibernate_sequence for update
Hibernate: update hibernate_sequence set next_val= ? where next_val=?
Hibernate: insert into article (article, created_time, folder_id, is_secret, modified_time, tag, title, id) values (?, ?, ?, ?, ?, ?, ?, ?)

在这里插入图片描述

  • 当不传主键,传入外键id/对象时
{
"title":"",
"article":"## markdown text",
"folderByFolderId":{
	"id":250,
	"isSecret":null,
	"folderName":"jsong"
	}
}

数据库插入一条有外键的数据,并不会更新关联的外键表数据

Hibernate: select next_val as id_val from hibernate_sequence for update
Hibernate: update hibernate_sequence set next_val= ? where next_val=?
Hibernate: insert into article (article, created_time, folder_id, is_secret, modified_time, tag, title, id) values (?, ?, ?, ?, ?, ?, ?, ?)

在这里插入图片描述

  • 当传入主键和外键时
{
"id":259,
"title":"",
"article":"## markdown text",
"folderByFolderId":
	{
		"id":250,
		"folderName":"jsong221s"
	}
}

可以同时更新表和外键关联表

Hibernate: select articleent0_.id as id1_0_1_, articleent0_.article as article2_0_1_, articleent0_.created_time as created_3_0_1_, articleent0_.folder_id as folder_i8_0_1_, articleent0_.is_secret as is_secre4_0_1_, articleent0_.modified_time as modified5_0_1_, articleent0_.tag as tag6_0_1_, articleent0_.title as title7_0_1_, folderenti1_.id as id1_1_0_, folderenti1_.created_time as created_2_1_0_, folderenti1_.folder_name as folder_n3_1_0_, folderenti1_.is_secret as is_secre4_1_0_, folderenti1_.modified_time as modified5_1_0_ from article articleent0_ left outer join folder folderenti1_ on articleent0_.folder_id=folderenti1_.id where articleent0_.id=?
Hibernate: update folder set created_time=?, folder_name=?, is_secret=?, modified_time=? where id=?
Hibernate: update article set article=?, created_time=?, folder_id=?, is_secret=?, modified_time=?, tag=?, title=? where id=?

在这里插入图片描述

persist

  • 当不传入id和外键id时

{“title”:"",“article”:"## markdown text"}

数据库插入一条没有外键的数据

Hibernate: select next_val as id_val from hibernate_sequence for update
Hibernate: update hibernate_sequence set next_val= ? where next_val=?
Hibernate: insert into article (article, created_time, folder_id, is_secret, modified_time, tag, title, id) values (?, ?, ?, ?, ?, ?, ?, ?)

在这里插入图片描述

  • 当不传主键,传入外键id/对象时
{
"title":"",
"article":"## markdown text",
"folderByFolderId":{
	"id":250,
	"isSecret":null,
	"folderName":"jsong"
	}
}

后台报错

org.hibernate.PersistentObjectException: detached entity passed to persist: com.jsong.wiki.backend.entity.FolderEntity

源码 调用save方法时,会判断是否是新实体

    public <S extends T> S save(S entity) {
        if (this.entityInformation.isNew(entity)) {
            this.em.persist(entity);
            return entity;
        } else {
            return this.em.merge(entity);
        }
    }

因为实体id为空,所以判断是新实体,但是当保存外键表时,会出现id冲突。

  • 当传入主键和外键时
{
"id":259,
"title":"",
"article":"## markdown text",
"folderByFolderId":
	{
		"id":250,
		"folderName":"jsong221s"
	}
}

更新本表,不更新外键表

Hibernate: select articleent0_.id as id1_0_0_, articleent0_.article as article2_0_0_, articleent0_.created_time as created_3_0_0_, articleent0_.folder_id as folder_i8_0_0_, articleent0_.is_secret as is_secre4_0_0_, articleent0_.modified_time as modified5_0_0_, articleent0_.tag as tag6_0_0_, articleent0_.title as title7_0_0_ from article articleent0_ where articleent0_.id=?
Hibernate: update article set article=?, created_time=?, folder_id=?, is_secret=?, modified_time=?, tag=?, title=? where id=?

你可能感兴趣的:(jpa)