Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-users] Weird behavior with CascadeType.REMOVE - deletes do not cascade depending on which fields are in a class

Hi,

The short version: I'm using EclipseLink (2.1.2) as my JPA provider, and I've run into a very weird behavior. For some reason, a ManyToOne relationship with CascadeType.ALL does not cascade deletes unless some fields (all relationship) are commented out.

Long version:

In my app, there's a Source class, which owns several other things (getters and setters omitted):

@MappedSuperclass
public abstract class IdentifiedEntity
{
    @Id
    @GeneratedValue( strategy = GenerationType.IDENTITY )
    private long localId;
}

@Entity
public class PartnerSource extends IdentifiedEntity
{
    @Column( unique = true, nullable = false )
    private String name;
    
    @OneToMany( mappedBy = "source", cascade = CascadeType.ALL, orphanRemoval = true )
    private List<Foo> foos = newArrayList( );
    
    @OneToMany( mappedBy = "source", cascade = CascadeType.ALL, orphanRemoval = true )
    private List<Bar> bars = newArrayList( );
    
    @OneToMany( mappedBy = "source", cascade = CascadeType.ALL, orphanRemoval = true )
    private List<Baz> bazes = newArrayList( );
}

Classes Foo, Bar and Baz all derive from the same class, SourceOwned:
@MappedSuperclass
public abstract class SourceOwned extends IdentifiedEntity
{
    public static final String COL_SOURCE = "SOURCE";
    public static final String COL_SOURCE_SPECIFIC_ID = "SOURCESPECIFICID";

    @ManyToOne( optional = false )
    @JoinColumn( nullable = false, name = COL_SOURCE )
    private Source source;

    @Column( name = COL_SOURCE_SPECIFIC_ID, nullable = false )
    private long sourceSpecificId;
}

@Entity
@Table( uniqueConstraints = @UniqueConstraint( 
    columnNames = { SourceOwned.COL_SOURCE, SourceOwned.COL_SOURCE_SPECIFIC_ID } ) )
public class Foo extends SourceOwned {  }

@Entity
@Table( uniqueConstraints = @UniqueConstraint( 
    columnNames = { SourceOwned.COL_SOURCE, SourceOwned.COL_SOURCE_SPECIFIC_ID } ) )
public class Bar extends SourceOwned {  }

@Entity
@Table( uniqueConstraints = @UniqueConstraint( 
    columnNames = { SourceOwned.COL_SOURCE, SourceOwned.COL_SOURCE_SPECIFIC_ID } ) )
public class Baz extends SourceOwned {  }
So far, so good. If I call entityManager.remove() on a Source object, I can see how EclipseLink deletes from the dependent tables before deleting the source:
[EL Fine]: DELETE FROM FOO WHERE (SOURCE = ?)
	bind => [1]
[EL Fine]: DELETE FROM BAR WHERE (SOURCE = ?)
	bind => [1]
[EL Fine]: DELETE FROM BAZ WHERE (SOURCE = ?)
	bind => [1]
[EL Fine]: DELETE FROM SOURCE WHERE (LOCALID = ?)
	bind => [1]
Unfortunately, reality isn't this simple, and Foo, Bar and Baz all need a bunch of OneToMany and OneToOne relationships to other tables. For Foo and Bar, this works fine, but when I add relationship fields to Baz and delete a Source, EclipseLink no longer attempts to delete from the BAZ table:
[EL Fine]: DELETE FROM FOO WHERE (SOURCE = ?)
	bind => [1]
[EL Fine]: DELETE FROM BAR WHERE (SOURCE = ?)
	bind => [1]
[EL Fine]: DELETE FROM SOURCE WHERE (LOCALID = ?)
	bind => [1]
Since there's a foreign key constraint on BAZ.SOURCE, deleting the source fails.

So what I'm wondering is, what on earth could it be with my relations that causes EclipseLink to ignore the cascades I've specified? I'm working on an SSCCE, but I've not yet been able to isolate this behaviour (my data model is quite large and highly proprietary) and wanted to check if this is some kind of known behavior (bug, PEBKAC indicator or whatever). TIA, /g


View this message in context: Weird behavior with CascadeType.REMOVE - deletes do not cascade depending on which fields are in a class
Sent from the EclipseLink - Users mailing list archive at Nabble.com.

Back to the top