0

I am using an Hibernate to manage the following relationship:

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

}

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

What I'm expecting is that a Dashboard can a list of Widgets. Each Widget belongs to One dashboard. However, the generated POJO looks like this:

{
  "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"
    }
  ]
}

For some reason It's adding "Widgets": as a proper POJO and then an additional "Dashwidgets": {}.

When I save the object, Only the DashWidgets object is saved.

What am I doing wrong?

user171943
  • 1,325
  • 3
  • 12
  • 18

1 Answers1

0

Take off the mappedBy from widgets. When you add `mappedBy' is actually gives the control of saving object to Child Entity.

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

Check out this question for more information.

Community
  • 1
  • 1
techtabu
  • 23,241
  • 3
  • 25
  • 34
  • Tried that, model is still coming out the same: `{ "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" } ] }` – user171943 Jun 22 '16 at 19:53
  • Before saving the object, set the parent on child as well. `widget.setDashWidget(dashboard)` for each child. You may need add setter method in the child. Alternatively, you can get rid of this parent object from Child and let the Hibernate to create a join table by itself from Parent. – techtabu Jun 22 '16 at 20:03