Skip to content

Rows of a OneToMany relationship are not deleted if it is defined in an embeddable #2411

Open
@StefanBauerTT

Description

@StefanBauerTT

Describe the bug
EclipseLink does not delete rows of a OneToMany relationship if is is defined in an embeddable.

To Reproduce
Steps/resources to reproduce the behavior:

  • EclipseLink version: 2.7.15
  • Java/JDK version: Eclipse Adoptium 21.0.5.11
  • Entity source (mainly applied annotations)
@Entity
public class Book {
	@Id
	String name = "";

	@Embedded
	PageSet pageSet = new PageSet();

	public Book(String name, List<Page> pages) {
		this.name = name;
		this.pageSet = new PageSet(pages);
	}

	protected Book() {
	}
}

@Embeddable
public class PageSet {
	@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
	List<Page> pages = Collections.emptyList();

	public PageSet(List<Page> pages) {
		this.pages = new ArrayList<>(pages);
	}

	protected PageSet() {
	}
}

@Entity
public class Page implements Serializable {
	@Id
	private long id;

	@Column
	String content = "";

	public Page(long id, String content) {
		this.id = id;
		this.content = content;
	}

	protected Page() {
	}
}

public class CollectionMappingDeleteTest {
	@Test
	void test() {
		Page page = new Page(100, "content");
		Book book = new Book("Name", List.of(page));

		tx(em -> em.persist(book));

		book.pageSet.pages.clear();

		tx(em -> em.merge(book));

		long pageCount =
				txWithRetval(em -> (long) em.createNativeQuery("SELECT COUNT(*) FROM PAGE").getSingleResult());

		assertThat(pageCount).isEqualTo(0);
	}
}

Expected behavior
Test passes.
When the pages of the PageSet get cleared, all rows should be deleted.

Actual behavior
Test fails. There is one row remaining.

Additional context

  • This bug is only triggered with FetchType.LAZY
  • This bug is only triggered when Static Weaving has happened
  • This bug is only triggered when the OneToMany relationship is inside of the embeddable. If the embeddable is "inlined", everything works as expected

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions