Hibernate ManyToOne重复关系

时间:2016-06-22 16:03:35

标签: java hibernate hibernate-mapping pojo

我正在使用Hibernate来管理以下关系:

Dashboard.java:

@Entity
@Table(name = "dashboard")
public class Dashboard {
    @Id
    @Column(name = "dashboard_id", nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    @Column(name = "type", nullable = false)
    private String type;
    @Column(name = "name", nullable = false)
    private String name;

    public String getName() {
        return name;
    }

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

    @OneToOne
    @JoinColumn(name = "user_id", updatable = false)
    private User user;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "dashwidgets")
    public List<Widget> widgets;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

}

和Widget.java

@Entity
@Table(name = "Widget")
public class Widget {
    @Id
    @Column(name = "widget_id", nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "widgetlayout_id")
    private WidgetLayout layout;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "dashboard_id")
    public Dashboard dashwidgets;

    public WidgetLayout getLayout() {
        return layout;
    }

    public void setLayout(WidgetLayout layout) {
        this.layout = layout;
    }

    @Column(name = "widget_type", nullable = false)
    private String widgetType;

    public Widget() {

    }

    public Widget(long id, String widgetType) {
        this.id = id;
        this.widgetType = widgetType;
    }

    @Column(name = "resizeable")
    public boolean resizable;

    @Column(name = "canpopout")
    public boolean canpopout;

    @Column(name = "settings", columnDefinition = "CLOB NOT NULL")
    public String settings;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getWidgetType() {
        return widgetType;
    }

    public void setWidgetType(String widgetType) {
        this.widgetType = widgetType;
    }
}

我期待的是仪表板可以列出小部件。每个窗口小部件属于一个仪表板。但是,生成的POJO如下所示:

{
  "Id": 0,
  "Type": "string",
  "Name": "string",
  "User": {
    "Id": 0,
    "Username": "string"
  },
  "Widgets": [
    {
      "Id": 0,
      "Layout": {
        "Id": 0,
        "Col": 0,
        "Row": 0,
        "Sizex": 0,
        "Sizey": 0
      },
      "Dashwidgets": {},
      "WidgetType": "string",
      "Resizable": false,
      "Canpopout": false,
      "Settings": "string"
    }
  ]
}

由于某种原因,它正在添加“Widgets”:作为一个合适的POJO,然后是一个额外的“Dashwidgets”:{}。

保存对象时,仅保存DashWidgets对象。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

从小部件中取消mappedBy。当你添加`mappedBy&#39;实际上是将对象保存到Child Entity。

@OneToMany(cascade = CascadeType.ALL)
public List<Widget> widgets;

查看this问题以获取更多信息。