2

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

Hibernate:更新participant_released set idt_released=null where idt_released=?

我该如何解决?如果列表为空,我想删除所有集合!

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

所有者.java

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

儿子.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;
}
}



例外:

 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
4

2 回答 2

1

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

也许你应该使用:

@OneToMany(cascade = {CascadeType.ALL, CascadeType.DELETE_ORPHAN})
@JoinColumn(name = "idt_released")
public List<ParticipationReleased> getParticipationReleases() {
    return participationReleases;
}
于 2013-09-23T21:06:52.117 回答
1

解决了!感谢大家的帮助!

@OneToMany(cascade = {CascadeType.ALL}, mappedBy="released")
@org.hibernate.annotations.Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
@JoinColumn(name = "idt_released", updatable = true, insertable = true)
public List<ParticipationReleased> getParticipationReleases() {
    return participationReleases;
}
于 2013-09-23T22:54:36.523 回答