gpt4 book ai didi

java - JPA orphanRemoval = true 尝试更新而不是删除

转载 作者:行者123 更新时间:2023-12-01 04:37:49 28 4
gpt4 key购买 nike

我有非常具体的情况,并尝试了不同的场景来实现从Java集合中删除时从数据库中删除。

我的 House 与 Room 具有 OneToMany 关系,而 Room 与 Bed 具有 OneToMany 关系。而且 House 和 Bed 之间也存在 OneToMany 关系:

房屋实体

public class HouHouse implements EntityInt, Serializable {
@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "HOU_HOUSES_SEQ")
private Long id;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "house", cascade = CascadeType.ALL, orphanRemoval = true)
@OrderBy("id")
private List<HouRoom> rooms;

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "id_house")
@OrderBy("id")
private List<HouBed> beds;
...
}

房间实体

public class HouRoom implements EntityInt, Serializable {
private static final long serialVersionUID = 1L;

@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "HOU_ROOMS_SEQ")
private Long id;

@NotNull
@ManyToOne
@JoinColumn(name = "id_house", nullable = false)
private HouHouse house;

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name="ID_ROOM")
@OrderBy("id")
private List<HouBed> beds;
}

床实体

public class HouBed implements EntityInt, Serializable {

private static final long serialVersionUID = 1L;

@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "HOU_BEDS_SEQ")
private Long id;

@NotNull
@ManyToOne
@JoinColumn(name = "id_room", nullable = false)
private HouRoom room;

@NotNull
@ManyToOne
@JoinColumn(name = "id_house", nullable = false)
private HouHouse house;
}

在这种情况下,当我尝试这样做时:

room.getBeds().remove(bed);
entityManager.merge(room);
entityManager.flush();

我收到一个错误:

06:28:53,344 INFO  [stdout] (http-localhost/127.0.0.1:8080-4) Hibernate: 
06:28:53,344 INFO [stdout] (http-localhost/127.0.0.1:8080-4) update
06:28:53,345 INFO [stdout] (http-localhost/127.0.0.1:8080-4) public.HOU_BEDS
06:28:53,345 INFO [stdout] (http-localhost/127.0.0.1:8080-4) set
06:28:53,345 INFO [stdout] (http-localhost/127.0.0.1:8080-4) ID_ROOM=null
06:28:53,345 INFO [stdout] (http-localhost/127.0.0.1:8080-4) where
06:28:53,345 INFO [stdout] (http-localhost/127.0.0.1:8080-4) ID_ROOM=?
06:28:53,345 INFO [stdout] (http-localhost/127.0.0.1:8080-4) and id=?

06:28:53,346 WARN [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost/127.0.0.1:8080-4) SQL Error: 0, SQLState: 23502
06:28:53,346 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost/127.0.0.1:8080-4) Zadanie wsadowe 0 update public.HOU_BEDS set ID_ROOM=null where ID_ROOM='20' and id='32' zostało przerwane. Wywołaj getNextException by poznać przyczynę.
06:28:53,346 WARN [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost/127.0.0.1:8080-4) SQL Error: 0, SQLState: 23502
06:28:53,346 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost/127.0.0.1:8080-4) ERROR: null value in column "id_room" violates not-null constraint
06:28:53,346 ERROR [org.hibernate.engine.jdbc.batch.internal.BatchingBatch] (http-localhost/127.0.0.1:8080-4) HHH000315: Exception executing batch [could not perform addBatch]
06:28:53,346 INFO [com.i4u.qla.action.house.HouRoomCrud] (http-localhost/127.0.0.1:8080-4) Start info here: ERROR: null value in column "id_room" violates not-null constraint
06:28:53,348 ERROR [stderr] (http-localhost/127.0.0.1:8080-4) javax.transaction.RollbackException: ARJUNA016053: Could not commit transaction.

所以 - jpa 尝试更新非空字段,而不仅仅是删除该行。

第二种情况 - 如果我将房间实体和床的关系更改为双向,那么在尝试从床集合中删除元素并合并房间后它什么也不做。 Hibernate 不会尝试删除或更新床:

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true, mappedBy="room")
@OrderBy("id")
private List<HouBed> beds;

如何管理它的工作?请帮忙。我花了很多时间尝试 StackOverflow 中的任何解决方案 - 但找不到任何对我的情况有帮助的解决方案。

最佳答案

不幸的是,当实体(实体列表)有多个父级时,会出现某种冲突。

我什至删除了 orphanRemoval 标志并尝试通过

手动删除子项(在我的示例中为 bed)

entityManager.remove(bed);

也没有成功。 JPA 没有尝试删除或更新它。

所以最后我删除了房子和床之间的 OneToMany 关系,只留下房间和床之间以及房子和房间之间的 OneToMany 关系 - 一切都开始工作(有很多“和”,我知道,抱歉)

关于java - JPA orphanRemoval = true 尝试更新而不是删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17079649/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com