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?