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:
Dateis 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@Immutablecovered; but, as@Immutablewas 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
MutabilityPlanand@Mutabilitywhich explicitly deal with (1).@Mutabilityis allowed on attributes, though not on an entity.In 6.2 (non-Final atm) we have added the ability to apply
@Immutableon 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
@Immutablesometimes means (1), sometimes (2) and sometimes both depending on where it is used. I saw a few options to achieve this consistency -@Immutablea shorthand for an immutableMutabilityPlan. For the notion of updateability I would have either relied on@Columnor introduced a new annotation (@Updateable?) specifically for case (2). Keeping in mind that allowing@Immutableon attributes is not in a Final release, there is no backwards compatibility concern here. I also like that the names actually reflect the intent.@Immutableas 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
@Immutableand@Mutability(and@Updateableif we elect that option) are valid or invalid based on where they are specified:Entity
Historically, Hibernate has allowed
@Immutableon an entity. I think the effect there is perfectly fine.@Mutabilityis never appropriate on an entity; Hibernate has very specific, internal requirements for theMutabilityPlanfor an entity.With either of the 2 options I listed:
@Immutablewould 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.@Mutabilitywould 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/Stringdiscussion. 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:
@Immutableis allowed and is shorthand for@Mutability(Immutability.class)plusColumn(..., updatable=false)on all columns associated with the attribute.@Mutabilityis allowed, supplying a customMutabilityPlanWith the clean room option:
@Immutableis allowed and only affects mutability - shorthand for@Mutability(Immutability.class)@Mutabilityis allowed, supplying a customMutabilityPlan@Updateable(false)is allowed and is shorthand forColumn(..., updatable=false)on all columns associated with the attributeWith the Zulip option:
@Immutableis 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.@Mutabilityis allowed, supplying a customMutabilityPlan@Updateable.@Updateable(false)is accomplished with@ImmutableSee also the discussions below about
AttributeConverterandUserTypeas they ultimately apply to attributes.AttributeConverter
An
AttributeConvertercan be marked as immutable which implies immutability to every attribute it is applied to. It is not reasonable for anAttributeConverterto be marked as non-updateable.With the clean room option:
@Immutableis allowed on the converter class and is shorthand for@Mutability(Immutability.class)@Mutabilityis allowed, supplying a customMutabilityPlanthat is applied to attributes the converter is applied to@Updateableis not allowedWith Zulip option:
@Immutableis not allowed on the converter class.@Mutabilityis allowed, supplying a customMutabilityPlanthat is applied to attributes the converter is applied toUserType
Same as
AttributeConverterBeta Was this translation helpful? Give feedback.
All reactions