2013-09-23 33 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; 
} 
相关问题