java - How to configure a polymorphic Map relationship with EBean? -
i have seen examples of setting map association ebean. examples i've seen typically support schema below.
with such schema, jpa mapping of video have property public list<videometadata> metadata;.
how can change metadata map instead of list, instance public map<string, string> metadata;?
create table video ( id uuid primary key, title varchar(255) not null ); create table metadata ( id bigserial primary key, video_id uuid not null, -- relationship not polymorphic key varchar(255) not null, value varchar(255) not null ); i have defined polymorphic relationship between metadata , video (and photo, , on...). video can have multiple metadata, can photo.
the polymorphic relationship configured inheritancetype.single_table , "describable" identified via combination of describable_type , describable_id.
the database schema looks this.
create table video ( id uuid primary key, title varchar(255) not null ); create table metadata ( id bigserial primary key, describable_id uuid not null, -- have polymorphism describable_type varchar(255) not null, -- have polymorphism key varchar(255) not null, value varchar(255) not null ); and models (in case it's relevant, i'm using ebean)...
package models; import io.ebean.model; import javax.persistence.id; import java.util.uuid; import play.data.validation.constraints; import javax.persistence.*; import java.util.list; @entity public class video extends model { @id public uuid id; @constraints.required public string title; @elementcollection(fetch = fetchtype.eager) @onetomany(mappedby = "describable", cascade = cascadetype.all) public list<videometadata> metadata; } package models; import io.ebean.model; import play.data.validation.constraints; import javax.persistence.*; @entity @inheritance(strategy = inheritancetype.single_table) @discriminatorcolumn( name = "describable_type", discriminatortype = discriminatortype.string ) public abstract class metadata extends model { @id @generatedvalue public long id; @constraints.required public string key; @constraints.required public string value; } package models; import javax.persistence.discriminatorvalue; import javax.persistence.entity; import javax.persistence.manytoone; @entity @discriminatorvalue("video") public class videometadata extends metadata { @manytoone(optional = false) public video describable; } package models; import javax.persistence.discriminatorvalue; import javax.persistence.entity; import javax.persistence.manytoone; @entity @discriminatorvalue("photo") public class imagemetadata extends metadata { @manytoone(optional = false) public photo describable; }
Comments
Post a Comment