gpt4 book ai didi

java - 为什么 hibernate 不删除我的空集合?

转载 作者:行者123 更新时间:2023-11-30 04:13:38 26 4
gpt4 key购买 nike

我有一个一对多的映射,但 hibernate 不尝试删除...而是删除,hibernate 尝试进行更新并将 null 设置为键...它抛出了一个异常:

Hibernate:更新participation_released set idt_released=null,其中idt_released=?

我该如何修复它?如果列表为空,我想删除所有集合!

实际上我的映射是这样的:

Owner.java

@OneToMany(cascade = {CascadeType.ALL})
@JoinColumn(name = "idt_released")
public List<ParticipationReleased> getParticipationReleases() {
return participationReleases;
}

Son.java

@Entity
@Table(name = "participation_released")
public class ParticipationReleased implements Serializable {

private static final long serialVersionUID = 1L;

private ParticipationReleasedPK participationReleasedPK;
private Released released;
private Colaborator colaborator;
private PerformanceType performanceType;

public ParticipationReleased() {
this(null, null, null);
}

public ParticipationReleased(Released released, Colaborator colaborator,
PerformanceType performanceType) {
super();
this.released = released;
this.colaborator = colaborator;
this.performanceType = performanceType;
}

public ParticipationReleased(ParticipationReleasedPK participationReleasedPK) {
super();
this.participationReleasedPK = participationReleasedPK;
}

@EmbeddedId
public ParticipationReleasedPK getParticipationReleasedPK() {
return participationReleasedPK;
}

public void setParticipationReleasedPK(
ParticipationReleasedPK participationReleasedPK) {
this.participationReleasedPK = participationReleasedPK;
}

@Transient
public Released getReleased() {
return released;
}

public void setReleased(Released released) {
this.released = released;
}

@ManyToOne(cascade = { CascadeType.REFRESH })
@JoinColumn(name = "idt_colaborator", insertable = false, updatable = false)
public Colaborator getColaborator() {
return colaborator;
}

public void setColaborator(Colaborator colaborator) {
this.colaborator = colaborator;
}

@ManyToOne(cascade = { CascadeType.REFRESH })
@JoinColumn(name = "idt_performance_type", insertable = false, updatable = false)
public PerformanceType getPerformanceType() {
return performanceType;
}

public void setPerformanceType(PerformanceType performanceType) {
this.performanceType = performanceType;
}
}

SonEmbeddableId.java

@Embeddable
public class ParticipationReleasedPK implements Serializable {

private static final long serialVersionUID = 1L;

private Integer idtReleased;
private Integer idtColaborator;
private Integer idtPerformanceType;

public ParticipationReleasedPK() {
}

public ParticipationReleasedPK(Integer idtReleased, Integer idtColaborator,
Integer idtPerformanceType) {
super();
this.idtReleased = idtReleased;
this.idtColaborator = idtColaborator;
this.idtPerformanceType = idtPerformanceType;
}

@Column(name = "idt_released", nullable = false)
public Integer getIdtReleased() {
return idtReleased;
}

public void setIdtReleased(Integer idtReleased) {
this.idtReleased = idtReleased;
}

@Column(name = "idt_colaborator", nullable = false)
public Integer getIdtColaborator() {
return idtColaborator;
}

public void setIdtColaborator(Integer idtColaborator) {
this.idtColaborator = idtColaborator;
}

@Column(name = "idt_performance_type", columnDefinition = "smallint", nullable = false)
public Integer getIdtPerformanceType() {
return idtPerformanceType;
}

public void setIdtPerformanceType(Integer idtPerformanceType) {
this.idtPerformanceType = idtPerformanceType;
}
}



异常(exception):

 Caused by: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:114)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:109)
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:244)
at org.hibernate.persister.collection.AbstractCollectionPersister.remove(AbstractCollectionPersister.java:1052)
at org.hibernate.action.CollectionUpdateAction.execute(CollectionUpdateAction.java:71)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:170)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:64)
at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:996)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1141)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:88)
... 119 more
Caused by: java.sql.BatchUpdateException: Column 'idt_released' cannot be null
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1666)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1082)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 133 more

最佳答案

您的问题是 ParticipationRelease 对象成为孤立对象,并且 CascadeType.ALL 不包含删除这些对象的操作。

也许你应该使用:

@OneToMany(cascade = {CascadeType.ALL, CascadeType.DELETE_ORPHAN})
@JoinColumn(name = "idt_released")
public List<ParticipationReleased> getParticipationReleases() {
return participationReleases;
}

关于java - 为什么 hibernate 不删除我的空集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18968541/

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