October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

You Cloned the Aggregate. Why Is Your Read-Only Operation Still Changing State?

A new aggregate instance is not necessarily an independent object graph. Learn how shared references, collection views, and ORM tracking can make a read-only operation appear to change state.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A new top-level object does not guarantee a separate object graph, and an API that looks read-only does not necessarily freeze data or prevent persistence. A shallow clone can share mutable children; an unmodifiable collection can remain a live view; and ORM read-only or no-tracking settings govern persistence behavior, not general in-memory mutability. To find the cause, trace object identity, collection boundaries, and ORM behavior separately.

What “clone” and “read-only” actually guarantee

These terms describe different boundaries. A clone may create a new outer object while retaining references to objects inside it. A collection wrapper may stop callers from changing membership through that wrapper while still reflecting changes made to the underlying collection. An ORM setting may change whether updates are detected or saved without preventing code from changing an object in memory.

As an Amazon Associate I earn from qualifying purchases.

So a state change can be real even when the operation appeared read-only: the change may be in memory, visible through an alias, or persisted later by an ORM. Those outcomes need separate checks.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why a cloned aggregate can still share state

In Java, the default Object.clone() behavior copies field contents as if by assignment. Oracle’s Java SE 21 API describes this as a shallow copy, not a deep copy: Object.clone(). Primitive fields are copied as values, but a reference field is copied as a reference. If that field points to a mutable child or collection, both outer objects can still point to the same thing.

For example, if an aggregate holds a mutable list of child entities, cloning the aggregate can produce a second aggregate whose list reference is the same as the original’s. Mutating a child through either aggregate then changes the same child. A new outer identity is not proof of independent nested state.

This describes Java’s documented default clone behavior; other languages, copy constructors, and custom clone methods may behave differently. Inspect the implementation rather than assuming that the word “clone” means recursive copying.

Why a read-only collection may still show changes

An unmodifiable collection view can reject mutation methods called on the view, yet remain connected to a backing collection. If another reference changes that backing collection, the view can reflect the update. Oracle’s Java SE 21 Collection documentation warns that an unmodifiable view is not necessarily immutable.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A defensive copy has a different boundary: it gives the caller a separate collection whose membership can be changed without changing the original collection. But copying membership alone does not copy mutable elements. If the returned list contains references to mutable children, callers can still change those children.

Some ORM models deliberately keep a collection in a backing field while exposing a defensive copy to application code. EF Core documents this pattern for collection navigations: Relationship navigations. It protects the collection’s membership through the returned copy; nested objects still need their own protection if they are mutable.

What ORM read-only settings do—and do not do

EF Core: tracking affects persistence, not object mutability

With a tracking query, EF Core keeps entity state in the context and can persist detected changes when SaveChanges is called. AsNoTracking is useful for read-only query scenarios because it skips normal context tracking, but it does not make the returned CLR objects immutable. Microsoft’s current EF Core tracking documentation also notes that entities included inside a custom projection can still be tracked by default: Tracking vs. No-Tracking Queries. Check the documentation and behavior for the EF Core version your project uses.

Hibernate: read-only entities can change in memory

Hibernate’s Session API says read-only entities can be modified, but changes to a field of a read-only entity are not made persistent: Session API. The detailed read-only chapter in the Hibernate ORM 4.3 documentation describes read-only entities as not being dirty-checked in the usual way for simple property changes: Read-only entities. That chapter is version-specific and older; verify the behavior against the Hibernate version in use.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Read-only status also does not by itself settle what happens to related objects. Hibernate association mappings determine cascade behavior, so inspect the mapping before concluding that an operation cannot affect related entities.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Trace the change in this order

  1. Compare identities. Inspect or log whether the original and clone refer to the same child object or collection. Comparing field values alone may miss shared references.
  2. Read the copy implementation field by field. Identify mutable references and decide whether each should be shared, shallow-copied, deeply copied, or represented by an immutable value.
  3. Inspect collection getters. Determine whether a caller receives the backing collection, an unmodifiable live view, or a fresh defensive copy. Then check whether the elements inside it are mutable.
  4. Locate the state change. Separate direct in-memory mutation from mutation through a shared reference, ORM relationship fix-up, and a database write at save or flush time.
  5. Check the ORM configuration for the project’s version. In EF Core, inspect tracking behavior and entities inside projections. In Hibernate, check read-only status and association cascades.
  6. Test memory and persistence separately. Assert identities and values before and after the operation, then independently verify whether a database write occurs. A changed object does not by itself prove that a database update was issued.

Choose a fix for the boundary you need

There is no single “read-only” fix because the required guarantee may concern collection membership, a complete object graph, in-memory mutation, persistence, or relationship cascades. Match the remedy to the boundary that is actually leaking.

Approach What it protects What it does not guarantee
Unmodifiable collection view Blocks collection mutators invoked through that view. It may reflect changes through the backing collection; mutable elements may still be changed.
Defensive collection copy Separates collection membership from the original collection. It does not deep-copy mutable elements unless they are copied too.
Deep copy Can separate nested mutable objects when the copy implementation recursively copies them. It requires explicit, correct handling of the graph; cost and compatibility depend on the model.
Immutable values or immutable model Can prevent mutation at the model boundary when nested state is immutable as well. A nominally immutable outer object is insufficient if it exposes mutable nested values.
ORM no-tracking or read-only configuration Changes framework tracking or persistence behavior according to that ORM and version. It does not generally freeze objects in memory or settle cascade behavior by itself.

If the caller needs a stable snapshot, copy or model the relevant graph so nested mutable references are not shared. If the caller only needs safe collection membership, a defensive collection copy may be enough. If the concern is a database update, inspect ORM tracking, save or flush behavior, and cascades rather than treating an object-copy change as the solution.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.