I need advice regarding a many-to-many relationship between KeyStoreEntity
and it's 3 child entities (KeyPairEntity, SecretKeyEntity, CertificateEntity
). I will use CertificateEntity
for this example as other 2 follow the same pattern/issue. My extended class BaseRdbmsEntity
is a @MappedSuperclass
with id, name and description attributes.
I had no problems with @ManyToMany
on both sides and using Map<String, CertificateEntity>
on KeystoreEntity
side to get certificates by their alias along with Map<String, KeyStoreEntity>
on CertificateEntity
to get the aliases and associated keystores.
Then I realized I'd blown it as a certificate could easily have the SAME alias in different keystores...thus CertificateEntity
map keys can clash and one value will wipe out subsequent value stored therein during a repository query.
I need to reverse the key/value types for the map attribute in CertificateEntity
to Map<KeyStoreEntity, String>
. It's unclear to me how I do a @MapKeyValue
(I realize this doesn't exist) to get alias as value in map...and not as key.
I've attempted to fix the problem by using @JoinTable
annotations but am not seeing an equivalent of @MapKeyColumn
for value-side of map. What I want is KeyStoreEntity
as key and String alias
as the value.
@Entity(name = "keystores")
@Table(name = "pki_keystores")
public class KeyStoreEntity extends BaseRdbmsSecurityEntity
{
@MapKeyColumn(name="alias")
@ManyToMany
(
fetch = FetchType.LAZY,
cascade = {CascadeType.PERSIST, CascadeType.MERGE}
)
private Map<String, CertificateEntity> certificates;
...snip...
}
@Entity(name = "certificates")
@Table(name = "pki_certificates", uniqueConstraints = {
@UniqueConstraint(columnNames = {"issuerDn", "serialNumber"})
})
public class CertificateEntity extends BaseRdbmsSecurityEntity
{
@MapKeyColumn(name="alias")
@ManyToMany(
fetch = FetchType.LAZY,
cascade = {CascadeType.PERSIST, CascadeType.MERGE},
mappedBy = "certificates",
targetEntity = KeyStoreEntity.class
)
private Map<String, KeyStoreEntity> keystores;
// Map type for above needs to be <KeyStoreEntity, String>
...snip...
}
I'm expecting a lazy fetch to work with k/v reversed on the CertificateEntity.keystores
side of the relationship. My expectation is I only need to tweak/add an annotation to CertificateEntity
for keystores
attribute.