Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
StefanBauerTT opened this issue May 7, 2025 · 1 comment

Comments

@StefanBauerTT
Copy link

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
@StefanBauerTT StefanBauerTT changed the title Rows of a OneToMany relationship are not deleted if is is defined in an embeddable Rows of a OneToMany relationship are not deleted if it is defined in an embeddable May 7, 2025
@StefanBauerTT
Copy link
Author

Out of curiosity, I just checked how a unidirectional OneToOne relationship inside an embeddable behaves.

@Embeddable
public class PageSet {
	@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
	Page pages = null;
...

There I see the same failure, i.e. the child entity does not get deleted if I set the reference in the parent to null. But in this case, the fetch type does not matter, it fails with both FetchType.LAZY and FetchType.EAGER. Please advise if you would like me to open a separate issue for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant