Soft Deletion Cascade with Eclipselink DescriptorCustomizer

Dear fellow programmers, I came to you for support with the following:

I use EclipseLink as ORM for my application and try to implement Soft Delete on one of my objects (articles) and cascade to delete these articles when the Catagory connection with OneToMany is removed.

When I delete an article, everything works as expected, but when I delete a category that has n articles, I get an exception. I am using DescriptorCustomizer to execute Soft Deletes.

DescriptorCustomizer (see org.eclipse.persistence.config.DescriptorCustomizer) with the following implementation:

public class ArticleCustomizer implements DescriptorCustomizer { @Override public void customize(ClassDescriptor classDescriptor) throws Exception { classDescriptor.getQueryManager().setDeleteSQLString("UPDATE article SET ACTIVE = '0' WHERE id = #id"); } } 

The objects I'm trying to save are:

AbstractItem

 @Entity @Table(name = "item") @Inheritance(strategy = InheritanceType.JOINED) public abstract class AbstractItem extends BaseEntity { @ManyToOne protected Category category; } 

Article

 @Entity @DiscriminatorValue("article") @Customizer(value=ArticleCustomizer.class) public class Article extends AbstractItem { } 

Category

 @Entity @Customizer(value=CategoryCustomizer.class) public class Category extends BaseEntity { @OneToMany(cascade = CascadeType.REMOVE) @JoinTable(name = "category_items", joinColumns = @JoinColumn(name = "category_id"), inverseJoinColumns = @JoinColumn(name = "item_id")) protected List<AbstractItem> items = new ArrayList<AbstractItem>(); @OneToMany(cascade = CascadeType.REMOVE) @JoinTable(name = "category_subcategories", joinColumns = @JoinColumn(name = "category_id"), inverseJoinColumns = @JoinColumn(name = "parent_category_id")) protected List<Category> categories = new ArrayList<Category>(); } 

An error occurred while trying to cascade a category that has n articles:

 Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'ACTIVE' in 'field list' Error Code: 1054 Call: UPDATE article SET ACTIVE = '0' WHERE id = ? bind => [null] 

I see that the identifier was null bound, however I don't understand why. Can someone help me in the right direction?

  • Eclipselink Version: 2.4.0
  • Spring: 3.1.2RELEASE

Thanks!

+4
source share
2 answers

I apologize for leaving your time. The solution was right in front of me:

 classDescriptor.getQueryManager().setDeleteSQLString("UPDATE item SET ACTIVE = '0' WHERE id = #ID"); 

The correct table for customizer should be ITEM instead of ARTICLE. Oddly enough, it worked to delete individual articles and did not work for cascades.

The second problem:

 #id instead of #ID 

It is case sensitive.

Thanks for helping me!

+3
source

Whenever a class implements DescriptorCustomizer , I suggest wrapping its encoding with:

 public static class MyCustomizer implements DescriptorCustomizer { @Override public void customize(ClassDescriptor descriptor) throws Exception { try { ... ... your functionality ... } catch (Exception e) { e.printStackTrace(); throw e; } } } 

The reason is that DescriptorCustomizer is silent when the logging options are below FINER (which is quite a lot). Then the exceptions do not apply to the "outside", and you will not receive notifications about them and can not find the cause of the failure.

Alternatively, you can set the EMF PersistenceUnitProperties.LOGGING_LEVEL property to SessionLog.FINER_LABEL .

0
source

Source: https://habr.com/ru/post/1441386/


All Articles