Skip to content

Instantly share code, notes, and snippets.

@MonkeyDAntoine
Last active February 8, 2019 15:29
Show Gist options
  • Select an option

  • Save MonkeyDAntoine/f0754bbd5f09b1144427df1bb63afb96 to your computer and use it in GitHub Desktop.

Select an option

Save MonkeyDAntoine/f0754bbd5f09b1144427df1bb63afb96 to your computer and use it in GitHub Desktop.
/*
CREATE TABLE `registrable_metadata` (
`owner` bigint(20) NOT NULL,
`name` varchar(255) NOT NULL,
`value` varchar(255) DEFAULT NULL,
primary key (owner, name)
);
create table registrable_seq (
next_val bigint null
);
*/
package com.wl.mts.tt.cm.id.issuer.connector.registry.data;
import java.io.Serializable;
import java.util.Map;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ConstraintMode;
import javax.persistence.ElementCollection;
import javax.persistence.Embeddable;
import javax.persistence.ForeignKey;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.MapKeyColumn;
import javax.persistence.MappedSuperclass;
import javax.persistence.SequenceGenerator;
import javax.validation.constraints.NotNull;
import com.google.common.collect.Maps;
import lombok.Builder;
import lombok.Data;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
@MappedSuperclass
/**
* Base class for registrable entities.
*/
@RequiredArgsConstructor
@Getter @Setter
public abstract class Registrable implements HasMetadata, Serializable {
@Id
@SequenceGenerator(
name="registrableSequence",
sequenceName="REGISTRABLE_SEQ"
) // Shared id generator between all Registrables
@GeneratedValue(generator = "registrableSequence", strategy = GenerationType.SEQUENCE)
private Long id;
@CollectionTable(
name = "registrable_metadata",
joinColumns = @JoinColumn(name = "owner"),
foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT)
) // Similar to OneToMany but reinforce composition association
@ElementCollection/*(targetClass = Metadata.class) <-- Unusable due to shared table */ // To manipule simple types instead of Metadata type
@MapKeyColumn(name = "name") // name as map key
@Column(name = "value") // value as map value
private Map<String,String> metadatas = Maps.newHashMap();
@Override
public String getMetadata(@NotNull String keyName) {
return metadatas.get(keyName);
}
@Embeddable
@Builder
@Data
public static class Metadata {
private Long owner;
private String name;
private String value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment