简体   繁体   English

JPA:是地图<Entity1,Set<Entity2> &gt; 可能吗?

[英]JPA: Is Map<Entity1,Set<Entity2>> possible?

I don't see an example anywhere so I am not sure this is possible.我在任何地方都没有看到示例,所以我不确定这是否可行。 But basically, I am trying to see if I can bind a field in an entity to但基本上,我想看看是否可以将实体中的字段绑定到

   Map<Skill,Set<Rating>> ratings;


CREATE TABLE Worker (
  ID         BIGINT PRIMARY KEY,

);

CREATE TABLE Skill (
  ID   BIGINT PRIMARY KEY,
  name VARCHAR(32) NOT NULL,
  UNIQUE (name)
);

CREATE TABLE WorkerSkillRating (
  ID       BIGINT PRIMARY KEY,
  WorkerID BIGINT NOT NULL,
  SkillID  BIGINT NOT NULL,
  Rating   INT,
  FOREIGN KEY (WorkerID) REFERENCES Worker (ID),
  FOREIGN KEY (SkillID) REFERENCES Skill (ID),
  FOREIGN KEY (Rating) REFERENCES Rating (ID)
);

CREATE TABLE Rating (
  ID       BIGINT PRIMARY KEY,
  score    TINYINT NOT NULL,
  comments VARCHAR(256)
);

Entities实体

@Entity
public class Skill {

    @Id
    private Long id;

    private String name;


    public Skill(String name) {
        this();
        this.name = name;
    }

    public Skill() {
        this.id = Math.abs( new Random().nextLong());
    }

}


@Entity
public class Worker {

    @Id
    private Long id;

    // The open question
    public Map<Skill, Set<Rating>> ratings;

}

@Entity
public class Rating {

    @Id
    private Long id;
    private Byte score;
    private String comments;

}

According to the JSR-0038 the JPA spec.根据 JSR-0038 JPA 规范。 When using Map, the following combination are just allowed: Basic Type, Entities and Embeddables.使用 Map 时,只允许以下组合:Basic Type、Entities 和 Embeddables。

Map<Basic,Basic> 
Map<Basic, Embeddable> 
Map<Basic, Entity>

Map<Embeddable, Basic> 
Map<Embeddable,Embeddable>
Map<Embeddable,Entity> 

Map<Entity, Basic>
Map<Entity,Embeddable>
Map<Entity, Entity> 

I don't think there is pretty much deal to have a possible mapping in the way that you want but that is out of the specs and most of the providers follow them, I think that mapping is not very common at all.我认为以您想要的方式进行可能的映射并没有什么大不了的,但这超出了规范,并且大多数提供商都遵循它们,我认为这种映射根本不是很常见。

"worker has many skills and he may have been given many ratings on a single skill. " “工人有很多技能,他可能在一项技能上得到了很多评级。”

Then add to the skill class a Set<Ratings> , instead of nested directly in the map as the value of it.然后在技能类中添加一个Set<Ratings> ,而不是直接嵌套在地图中作为它的值。

It might not answer your question with the map but...它可能无法用地图回答您的问题,但是...

It looks like your rating table is unnecessary.看起来您的评分表是不必要的。

You could instead have你可以改为

CREATE TABLE Worker (
  ID         BIGINT PRIMARY KEY,
);

CREATE TABLE Skill (
  ID   BIGINT PRIMARY KEY,
  name VARCHAR(32) NOT NULL,
  UNIQUE (name)
);

CREATE TABLE WorkerSkill (
  ID       BIGINT PRIMARY KEY,
  WorkerID BIGINT NOT NULL,
  SkillID  BIGINT NOT NULL,
  score    TINYINT NOT NULL,
  comments VARCHAR(256)
  FOREIGN KEY (WorkerID) REFERENCES Worker (ID),
  FOREIGN KEY (SkillID) REFERENCES Skill (ID)
);

Note I moved the rating information to WorkerSkill table.注意我将评级信息移至 WorkerSkill 表。

Then you can map your entities per below然后你可以按照下面的映射你的实体

@Entity
public class Skill {

    @Id
    private Long id;

    private String name;

    // Getter setters const etc

}

@Entity
public class WorkerSkill {

    @Id
    private Long id;

    private int score;

    private String comments;

    @ManyToOne
    private Skill skill;

    @ManyToOne        
    private Worker worker;

    // Getter setters const etc

}


@Entity
public class Worker {

    @Id
    private Long id;

    @OneToMany
    public List<WorkerSkill> workerSkills = new ArrayList<>();

    // Getter setters const etc

}

Then you can access all worker's skill using worker.getWorkerSkill();然后你可以使用worker.getWorkerSkill();访问所有工人的技能worker.getWorkerSkill();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM