Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » soft delete is deleting relations
soft delete is deleting relations [message #731366] Fri, 30 September 2011 18:28 Go to next message
Luiz E. is currently offline Luiz E.Friend
Messages: 97
Registered: September 2010
Member
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 18:29]

Report message to a moderator

Re: soft delete is deleting relations [message #731890 is a reply to message #731366] Mon, 03 October 2011 12:14 Go to previous messageGo to next message
Luiz E. is currently offline Luiz E.Friend
Messages: 97
Registered: September 2010
Member
anyone?
Re: soft delete is deleting relations [message #731901 is a reply to message #731366] Mon, 03 October 2011 12:14 Go to previous messageGo to next message
Luiz E. is currently offline Luiz E.Friend
Messages: 97
Registered: September 2010
Member
anyone?
Re: soft delete is deleting relations [message #731983 is a reply to message #731890] Mon, 03 October 2011 15:36 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

How did you configure the class to use soft deletes?

I assume you overwrote the deleteSQL to instead be an update for the class. You will also need to do something similar for the aliquotas mapping (ManyToManyMapping also has a deletSQL and deleteAllSQL, and selectSQL).


James : Wiki : Book : Blog : Twitter
Re: soft delete is deleting relations [message #732007 is a reply to message #731983] Mon, 03 October 2011 16:40 Go to previous messageGo to next message
Luiz E. is currently offline Luiz E.Friend
Messages: 97
Registered: September 2010
Member
I used a customizer like this

descriptor.getQueryManager().setDeleteSQLString("update ecf.ImpressoraFiscal set ativa = false where id = #id");


so, where am i going to overwrite this sqlString for my mapping? in aliquota class? you said 'aliquotas mapping'. should i overwrite it on the mapping?

thanks a lot
Re: soft delete is deleting relations [message #734052 is a reply to message #732007] Thu, 06 October 2011 14:21 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

Yes in your mapping.

((ManyToManyMaping)descriptor.getMappingForAttributeName("aliquotas")).setDeleteSQLString(...)


James : Wiki : Book : Blog : Twitter
Re: soft delete is deleting relations [message #734057 is a reply to message #732007] Thu, 06 October 2011 14:21 Go to previous messageGo to next message
James is currently offline JamesFriend
Messages: 272
Registered: July 2009
Senior Member
Yes in your mapping.

((ManyToManyMaping)descriptor.getMappingForAttributeName("aliquotas")).setDeleteSQLString(...)
--
James : http://wiki.eclipse.org/EclipseLink : http://en.wikibooks.org/wiki/Java_Persistence : http://java-persistence-performance.blogspot.com/
Re: soft delete is deleting relations [message #1738478 is a reply to message #731366] Wed, 20 July 2016 08:19 Go to previous message
Roberto Pierpaoli is currently offline Roberto PierpaoliFriend
Messages: 1
Registered: July 2016
Junior Member
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.
Previous Topic:ClassDescriptor.getPrimaryKeyFieldNames() (Dynamic MOXy)
Next Topic:Persist @ElementCollection Map - The list of fields to insert into the table is empty
Goto Forum:
  


Current Time: Tue Jul 26 14:18:30 GMT 2016

Powered by FUDForum. Page generated in 0.04299 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software