I’ve got an entity that is identified by an uniqueIdentifier and version, using both in an @Embeddable
primary key. To identify which version is currently “active”, there is also a boolean flag with that name (the rule is that only one row with a given uniqueIdentifier may have the active flag set).
@Embeddable
class PrimaryKey {
private long uniqueIdentifier;
private int version;
// etc.
}
@Entity
class Entity {
@Id
private PrimaryKey id;
// etc.
private boolean active; // only true for at max 1 version for any given uniqueIdentifier
}
So far, so easy. This works fine with Hibernate and allows me to query either any given version (WHERE uniqueIdentifier = ? and version = ?
) or whatever version is current (WHERE uniqueIdentifier = ? and active = true
).
I also got another entity that contains my Entity…
@Entity
class ParentEntity {
@OneToOne
private Entity entity;
}
Obviously, the table for ParentEntity
contains two columns like entity_uniqueIdentifier
and entity_version
.
Now, what I would like to do is the make the entity_version
column of the ParentEntity
nullable for business reasons. The desired behaviour would be as follows…
- When I load a
ParentEntity
where theentity_version
is notnull
, load the entity with the given version - When I load a
ParentEntity
where theentity_version
isnull
, load the entity where the active flag is true.
Managing the field (setting it, etc.) is not the problem here, my only problem is when loading it. Does anyone know how to do this with Hibernate? Is it even possible?