soft delete is deleting relations [message #731366] |
Fri, 30 September 2011 14:28  |
Eclipse User |
|
|
|
hi all
i have entities like this
@Entity
@Table(schema=Schemas.ECF)
@Index(name="id_impressora_fiscal", columnNames={"marca","modelo","numeroFabricacao"})
@Customizer(value=ImpressoraFiscalCustomizer.class)
public class ImpressoraFiscal implements Serializable{
private static final long serialVersionUID = 6246259319048104042L;
@OneToMany(cascade={CascadeType.PERSIST}, orphanRemoval=false)
@JoinTable(schema=Schemas.ECF)
private List<Aliquota> aliquotas = new ArrayList<Aliquota>();
and
@Entity
@Table(schema=Schemas.ECF)
public class Aliquota implements Serializable {
private static final long serialVersionUID = -1473589350884030504L;
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Column(nullable=false)
private Double valor;
my 'ImpressoraFiscal' class is using soft delete, where i just set a boolean value to false, turning it to disabled.
but as you can see on sql logs, eclipselink is deleting all the data from the relational table
[EL Fine]: 2011-09-30 18:28:04.5--Connection(7033304)--Thread(Thread[main,6,main])--DELETE FROM ecf.IMPRESSORAFISCAL_ALIQUOTA WHERE (ImpressoraFiscal_ID = ?)
[EL Fine]: 2011-09-30 18:28:04.515--Connection(7033304)--Thread(Thread[main,6,main])-- bind => [1]
[EL Fine]: 2011-09-30 18:28:04.515--Connection(7033304)--Thread(Thread[main,6,main])--update ecf.ImpressoraFiscal set ativa = false where id = ?
[EL Fine]: 2011-09-30 18:28:04.515--Connection(7033304)--Thread(Thread[main,6,main])-- bind => [1]
what i am missing?
thanks a lot!
[Updated on: Fri, 30 September 2011 14:29] by Moderator
|
|
|
|
|
|
|
|
|
Re: soft delete is deleting relations [message #1738478 is a reply to message #731366] |
Wed, 20 July 2016 04:19  |
Eclipse User |
|
|
|
Hi all,
I'm reopening this very old thread to share my recent experience: the approach described int the latest post didn't work for me using EclipseLink 2.6.3.
The point is the "earlyPredelete" method of the class org.eclipse.persistence.mappings.ManyToManyMapping: as stated in the method's comment that method
"Delete join tables before the start of the deletion process to avoid constraint errors."
The method internally checks for "isCascadeOnDeleteSetOnDatabase": if set, the deletion is jumped (I think it assumes that the DB will handle it by itself).
So, I did the following:
@Override
public void customize(ClassDescriptor descriptor) throws Exception
{
log.debug("EclipseLink Customizer: setting redirectors and 'hacks' for logical delete on table {}",
descriptor.getTableName());
descriptor.setDefaultDeleteObjectQueryRedirector(new LogicalDeleteRedirector());
descriptor.getMappings().forEach((map) ->
{
if (map instanceof ManyToManyMapping)
{
log.debug("Preventing pre-delete on join table for ManyToMany mapping {}", map.getAttributeName());
// hack
((ManyToManyMapping) map).setIsCascadeOnDeleteSetOnDatabase(true);
}
});
}
Inside this customizer I set a QueryRedirector on the entity's descriptor (it handles the logical delete for the main table) and I fake the "isCascadeOnDeleteSetOnDatabase" for the ManyToMany mappings: this prevents the "earlyPredelete" operation on the related join tables (apparently without drawbacks... so far).
The final result is that entities are soft deleted and relationships are kept, that was my goal.
I'll be happy to have your opinion on the approach, expecially if you foresee possibile problems that I'm currently missing.
Thanks in advance.
|
|
|
Powered by
FUDForum. Page generated in 0.05407 seconds