2013-09-23 34 views
1

我有一對一的映射,但休眠不要嘗試刪除...相反,刪除,休眠試圖做一個更新,並將null設置爲keys..it拋出一個異常:爲什麼hibernate不刪除我的空集合?

Hibernate :更新participation_released設置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; 
} 
} 



例外:

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 
+0

將空集合設置爲字段而不是空值。 –

+0

它已經是一個空集合,但它不斷嘗試更新!我試着用「released.setParticipationReleases(null);」和「released.setParticipationReleases(new ArrayList ());」 ...但沒有成功:( – placplacboom

回答

1

您的問題是ParticipationRelease對象成爲孤兒,CascadeType.ALL不包括刪除這些對象的動作。

也許你應該使用:

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

同樣的問題: @OneToMany @Cascade({org.hibernate.annotations.CascadeType.ALL,org.hibernate.annotations.CascadeType.DELETE_ORPHAN}) @JoinColumn(NAME =「 ) – placplacboom

+0

嘗試使用:@JoinColumn(name =「idt_released」,updatable = true,insertable = true) 如果問題仍然存在,可以顯示錯誤跟蹤以獲取更多信息 –

+0

同樣的問題! SonEmbeddableId – placplacboom

0

解決了!感謝所有幫助!

@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; 
} 
相關問題