3

I got several Projects with an ID and a Name. A Project contains several Models, while each Model contains several Simulations.

I thought about saving a HashMap in my Project entity

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

But I actually have no clue what Annotation to use.. I tried @CollectionOfElements but I just got this exception:

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

Project:

@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;
}
}

Simulation

@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);
}
}

Model

@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();
}
}

This is what I want to have as tables:

Project

ID Name

1 Project A

2 Project B

Model

ID Name

1 Model A

2 Model B

Simulation

ID Name

1 Simulation A

2 Simulation B

3 Simulation C

IntermediateTable

ProjectID ModelID SimulationID

1 -- 1 -- 1

1 -- 2 -- 2

1 -- 1 -- 3

monti
  • 455
  • 7
  • 25
  • I'm not sure hibernate can do That, this is not the way we use to create association. But there a some way to associate directly to a Map in a different way you try to do, see that answer: http://stackoverflow.com/questions/7876724/how-to-return-mapkey-value-with-hql – pdem Feb 09 '15 at 13:15
  • 1
    The JPA specifciation does not mandate that mappings of type Map>should be supported and I am not aware of any provider specific solutions. There is however some discussion here on implementing in HIbernate using some custom code: http://blog.xebia.com/2007/10/05/mapping-multimaps-with-hibernate/ – Alan Hay Feb 09 '15 at 14:11
  • While there seem to be no solution I decided to code an workaround with an helper class which holds the simulations, one project and one model. – monti Feb 10 '15 at 10:08

0 Answers0