java - 没有bean的映射表

标签 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

我的问题是,如何在练习类(class)中映射它?因为我不想创建一个ExerciseParamter 类...

谢谢!

最佳答案

这是一个多对多关系。您可以使用 @ManyToMany 注释来做到这一点。

将以下注释添加到 Employee 实体的 getParameters 方法

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

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

@ManyToMany(mappedBy="parameters")

关于java - 没有bean的映射表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32810670/

相关文章:

java - Hibernate OneToMany 列表为空

java - 无法恢复嵌套 fragment 状态

java - 与依赖的 Eclipse 项目绑定(bind)不匹配

java - Spring Boot 和 Elastic 客户端 - NoSuchMethodError : org. elasticsearch.action.support.IndicesOptions.ignoreThrottled()Z

java - 尽管缺口如何全屏显示?

java - Apache HTTP 客户端相当于配置 shapefile 的 CURL 命令

hibernate - 找不到表; SQL语句

java - "No Persistence Unit Found"错误

java - Hibernate Session 更新未反射(reflect)在数据库中

hibernate - 保存时将Grails gorm舍入 float