没有bean的地图表

时间:2015-09-27 17:21:07

标签: java hibernate

我不确定标题是否正确......但这是我的问题

这是我的bean类

@Entity
@Table(name = "Exercise")
public class Exercise {
    private IntegerProperty exerciseID;
    private ObjectProperty<String> name;
    private ObjectProperty<ExerciseCategory> category;
    private ObservableList<Parameter> parameters;

    public Exercise(int exerciseID, String name, ExerciseCategory category){
        this.exerciseID = new SimpleIntegerProperty(exerciseID);
        this.name = new SimpleObjectProperty<>(name);
        this.category = new SimpleObjectProperty<>(category);
        parameters = FXCollections.observableArrayList();
    }

    public Exercise(){
        this(0,null, null);
    }

    public Exercise(String name, ExerciseCategory category){
        this(0, name, category);
    }

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

    public IntegerProperty exerciseIDProperty() {
        return exerciseID;
    }

    public void setExerciseID(int exerciseID) {
        this.exerciseID.set(exerciseID);
    }

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

    public ObjectProperty<String> nameProperty() {
        return name;
    }

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

    @OneToOne
    public ExerciseCategory getCategory() {
        return category.get();
    }

    public ObjectProperty<ExerciseCategory> categoryProperty() {
        return category;
    }

    public void setCategory(ExerciseCategory category) {
        this.category.set(category);
    }

    public ObservableList<Parameter> getParameters() {
        return parameters;
    }

    public void setParameters(ObservableList<Parameter> parameters) {
        this.parameters = parameters;
    }
}

一个练习可以有一个参数。此映射保存在ExerciseParameter表中,如下所示:

ExerciseParameter
ExerciseID int(11) PK
ParameterID int(11) PK

我的问题是,我如何在练习课中映射这个?因为我不想制作一个ExerciseParamter类......

谢谢!

1 个答案:

答案 0 :(得分:2)

这是many-to-many关系。您可以使用@ManyToMany注释来执行此操作。

将以下注释添加到getParameters实体

Employee方法中
@ManyToMany(cascade = {CascadeType.ALL})
@JoinTable(name="ExerciseParameter", 
            joinColumns={@JoinColumn(name="ExerciseID ")}, 
            inverseJoinColumns={@JoinColumn(name="ParameterID")})

并将以下注释添加到getEmployees实体的Parameter方法中。 (您尚未提供Parameter实体。我认为Parameter类包含getEmployees方法。)

@ManyToMany(mappedBy="parameters")