Mutability consistency #6090
Replies: 4 comments 25 replies
-
I may be misunderstanding something, but in my mind immutability may imply non-updateability, but not always. In theory, and lacking any input from users, updateability would be a direct consequence of "aggregated" immutability... What I mean is, for example, with a model like this:
Then if all of the following are considered immutable:
... we can safely consider the column(s) bound to Though I'll admit I don't fully grasp what's at stake here. In particular, I don't understand why one would want to force non-updateability on a mutable attribute/type... does that even make sense? Why not declare that attribute/type immutable? |
Beta Was this translation helpful? Give feedback.
-
The conclusion after discussions with others on the team is to drop the implication that a Does it make sense to provide an |
Beta Was this translation helpful? Give feedback.
-
Ahah, I think I might have figured out a major reason for us talking past each other on this stuff. Currently, And so, at least from the point of view of the user, non-updatability always implies immutability, even though there’s no explicit The funny thing is that after much discussion on Zulip, this was essentially the semantic we converged on. Without realizing, it seems to me, that this is the semantic we already have. |
Beta Was this translation helpful? Give feedback.
-
I'm also doing nothing else with |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hibernate has different notions of (im)mutability:
Date
is mutable because one can change its internal state viaDate#setTime
. AString
, however, is immutable because there is no way to change its internal state. This sense describes part of the metadata for the Java model and has an affect on dirty check, caching and making copies.@Column(updatable=true/false)
for all associated columns. This sense is a function of the O/R mapping.For the rest of the Discussion, to avoid confusion, let's call (1) mutability and (2) updateability.
Historically Hibernate had
@Immutable
. It was actually never well defined which of the two@Immutable
covered; but, as@Immutable
was only allowed on an entity, this distinction was not super important - an entity we are told is immutable is naturally non-updateable as well since we never update those columns.In 6.0 we introduced
MutabilityPlan
and@Mutability
which explicitly deal with (1).@Mutability
is allowed on attributes, though not on an entity.In 6.2 (non-Final atm) we have added the ability to apply
@Immutable
on attributes. The current effect of that is that the attribute is considered both immutable (1) and non-updateable (2).The consistency problem is that currently
@Immutable
sometimes means (1), sometimes (2) and sometimes both depending on where it is used. I saw a few options to achieve this consistency -@Immutable
a shorthand for an immutableMutabilityPlan
. For the notion of updateability I would have either relied on@Column
or introduced a new annotation (@Updateable
?) specifically for case (2). Keeping in mind that allowing@Immutable
on attributes is not in a Final release, there is no backwards compatibility concern here. I also like that the names actually reflect the intent.@Immutable
as always meaning both. The case of immutable but updateable would have to explicitly use@Mutability
. This has a big impact, including some backwards compatibility concerns.Where each is valid
Each of
@Immutable
and@Mutability
(and@Updateable
if we elect that option) are valid or invalid based on where they are specified:Entity
Historically, Hibernate has allowed
@Immutable
on an entity. I think the effect there is perfectly fine.@Mutability
is never appropriate on an entity; Hibernate has very specific, internal requirements for theMutabilityPlan
for an entity.With either of the 2 options I listed:
@Immutable
would remain valid on the root of an entity hierarchy and continues to mean that Hibernate ignores any attribute state changes to the entities in that hierarchy. It effectively becomes read only.@Mutability
would remain invalid on an entityAttribute
An attribute can be either, neither or both. It is mutable/immutable independent of being updateable/non-updateable.
Non-updateable implies immutable.
Generally the mutability of an attribute is dictated by its Java type, as in the
Date
/String
discussion. There are times a user might want to make an attribute, with an inherently mutable type (e.g.Date
), immutable. E.g. this has some performance gains for the expectation that Hibernate ignores internal state changes.A non-updateable attribute means that none of the columns associated with the attribute are considered updateable. Note that the effective impact of this might vary depending on the type of attribute (basic versus many-to-one e.g.).
Again, this was added in 6.2 which is not yet Final, so we have some leeway here with regard to backwards compatibility.
Currently:
@Immutable
is allowed and is shorthand for@Mutability(Immutability.class)
plusColumn(..., updatable=false)
on all columns associated with the attribute.@Mutability
is allowed, supplying a customMutabilityPlan
With the clean room option:
@Immutable
is allowed and only affects mutability - shorthand for@Mutability(Immutability.class)
@Mutability
is allowed, supplying a customMutabilityPlan
@Updateable(false)
is allowed and is shorthand forColumn(..., updatable=false)
on all columns associated with the attributeWith the Zulip option:
@Immutable
is allowed and is shorthand for@Mutability(Immutability.class)
plusColumn(..., updatable=false)
on all columns associated with the attribute (tbf I only verified basic). This is how it worked in the previous 6.2 CR releases.@Mutability
is allowed, supplying a customMutabilityPlan
@Updateable
.@Updateable(false)
is accomplished with@Immutable
See also the discussions below about
AttributeConverter
andUserType
as they ultimately apply to attributes.AttributeConverter
An
AttributeConverter
can be marked as immutable which implies immutability to every attribute it is applied to. It is not reasonable for anAttributeConverter
to be marked as non-updateable.With the clean room option:
@Immutable
is allowed on the converter class and is shorthand for@Mutability(Immutability.class)
@Mutability
is allowed, supplying a customMutabilityPlan
that is applied to attributes the converter is applied to@Updateable
is not allowedWith Zulip option:
@Immutable
is not allowed on the converter class.@Mutability
is allowed, supplying a customMutabilityPlan
that is applied to attributes the converter is applied toUserType
Same as
AttributeConverter
Beta Was this translation helpful? Give feedback.
All reactions