Hibernate:带有对象注释列表的HashMap

时间:2015-02-09 12:27:55

标签: java hibernate annotations

我有几个项目IDName。项目包含多个模型,而每个模型包含多个模拟。

我考虑过在我的项目实体中保存HashMap

private HashMap<Model, ArrayList<Simulation>> modelSimulation = new HashMap<>();

但实际上我不知道要使用什么注释..我试过@CollectionOfElements但是我得到了这个例外:

Illegal attempt to map a non collection as a @OneToMany..

项目:

@Entity
public class Project {
private final IntegerProperty pID = new SimpleIntegerProperty();
private final StringProperty name = new SimpleStringProperty();

private HashMap<Model, ArrayList<Simulation>> modelSimulation = new HashMap<>();

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getsID() {
    return pID.get();
}

@Column(name = "NAME")
public String getName() {
    return name.get();
}


public HashMap<Model, ArrayList<Simulation>> getModelSimulation() {
    return modelSimulation;
}

public void setModelSimulation(
        HashMap<Model, ArrayList<Simulation>> modelSimulation) {
    this.modelSimulation = modelSimulation;
}
}

模拟

@Entity
public class Simulation {
private final IntegerProperty sID = new SimpleIntegerProperty();
private final StringProperty name = new SimpleStringProperty();

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getsID() {
    return sID.get();
}

@Column(name = "NAME")
public String getName() {
    return name.get();
}

public void setName(String name) {
    this.name.set(name);
}
}

模型

@Entity
public class Model {
private final IntegerProperty mID = new SimpleIntegerProperty();
private final StringProperty name = new SimpleStringProperty();

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getsID() {
    return mID.get();
}

@Column(name = "NAME")
public String getName() {
    return name.get();
}
}

这就是我想要的表格:

项目

ID名称

1项目A

2项目B

模型

ID名称

1模型A

2模型B

模拟

ID名称

1模拟A

2模拟B

3模拟C

IntermediateTable

ProjectID ModelID SimulationID

1 - 1 - 1

1 - 2 - 2

1 - 1 - 3

0 个答案:

没有答案