Posted on

JPA MapKeyColumn Annotation Example

email_map table:

CREATE TABLE `email_map` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`emailid` bigint(20) NOT NULL,
`key` varchar(48) DEFAULT NULL,
`value` varchar(128) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `emailid` (`emailid`,`key`),
KEY `fk_email_map_email` (`emailid`),
CONSTRAINT `fk_email_map_1` FOREIGN KEY (`emailid`) REFERENCES `email` (`emailid`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2953 DEFAULT CHARSET=utf8;

Inside email entity:

    @ElementCollection
@CollectionTable(name = "email_map", joinColumns =
@JoinColumn(name = "emailid"))
@MapKeyColumn(name = "key")
@Column(name = "value")
private Map<String, String> values;

public Map<String, String> getValues() {
return values;
}

public void setValues(Map<String, String> values) {
this.values = values;
}
Leave a Reply

Your email address will not be published. Required fields are marked *