如何配置与EBean的多态Map关系?

时间:2017-09-11 03:47:30

标签: java jpa polymorphic-associations ebean

我已经看到了使用EBean设置Map关联的示例。我见过的示例通常支持如下的架构。

使用这样的架构,Video的JPA映射将具有属性public List<VideoMetadata> metadata;

如何更改此设置,以便metadataMap而不是List,例如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, -- this relationship is not polymorphic
  key VARCHAR(255) NOT NULL,
  value VARCHAR(255) NOT NULL
);

我已经定义了元数据和视频(和Photo,以及...)之间的多态关系。视频可以有多个元数据,照片也可以。

多态关系配置为InheritanceType.SINGLE_TABLE和&#34;可描述的&#34;通过describable_typedescribable_id的组合来识别。

数据库架构如下所示。

CREATE TABLE video (
  id UUID PRIMARY KEY,
  title VARCHAR(255) NOT NULL
);

CREATE TABLE metadata (
  id BIGSERIAL PRIMARY KEY,
  describable_id UUID NOT NULL, -- now we have polymorphism
  describable_type VARCHAR(255) NOT NULL, -- now we have polymorphism
  key VARCHAR(255) NOT NULL,
  value VARCHAR(255) NOT NULL
);

模型(如果相关,我使用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;

}

0 个答案:

没有答案