{ "id" :"3",
"userId": "abc",
"favName": "shashank",
"kpiName": "FavKPI",
"rptId": "529",
"language": "EN",
"selectedControlIdList": [
{
"favouriteId": 3,
"controlId": "3",
"controlName": " ",
"label": "Plant",
"keyValue": "KPI_01_PL_01_1",
"structureType": "LISTBOX"
},
{
"favouriteId": 3,
"controlId": "2",
"controlName": " ",
"label": "Plant12",
"keyValue": "KPI_01",
"structureType": "LISTBOX"
}
]
}
我的控制器类是
@RequestMapping(value = "/addFavData", method = RequestMethod.POST, consumes =MediaType.APPLICATION_JSON_VALUE, produces =MediaType.APPLICATION_JSON_VALUE)
public void addFavData(@RequestBody FavouriteDTO requestInputMapper) {
favouriteService.addFavouriteData(requestInputMapper);
}
服务等级
public void addFavouriteData(FavouriteDTO requestInputMapper)
{
favouriteRepository.save(requestInputMapper);
}
这些是实体类!
@Entity
@Table(name = "favorite", schema = "quality")
public class FavouriteDTO implements Serializable{
private static final long serialVersionUID = -7089417397407966229L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "userId")
private String userId;
@Column(name = "favName")
private String favName;
@Column(name = "kpiName")
private String kpiName;
@Column(name = "rptId")
private String rptId;
@Column(name = "language")
private String language;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "favouriteId")
private List<DefaultControlsDTO> selectedControlIdList;
}
和
@Entity
@Table(name = "favoriteControls", schema = "quality")
public class DefaultControlsDTO implements Serializable {
private static final long serialVersionUID = 8720721227933753311L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "favouriteId")
private Integer favouriteId;
@Column(name = "controlId")
private String controlId;
@Column(name = "controlName")
private String controlName;
@Column(name = "label")
private String label;
@Column(name = "keyValue")
private String keyValue;
@Column(name = "structureType")
private String structureType;
}
此处ID是自动生成的。并且favouriteId与id相同。 所以我该如何存储数据,因为id是自动生成的,因此我需要输入与id相同的收藏ID。所以我如何将数据存储在数据库中 所以我给了我的实体课。我有两个实体收藏夹和DefaultFavuorite实体。所以我该如何存储数据
答案 0 :(得分:0)
您可以告诉Hibernate和任何其他JPA实现,将您在实体上执行的某些操作级联到与其关联的子实体。唯一要做的就是定义要级联到子实体的操作类型。
下面的代码片段显示了一个示例,其中我将Author实体的持久操作层叠到所有关联的Book实体。
@Entity
public class Author {
…
@ManyToMany(mappedBy=”authors”, cascade = CascadeType.PERSIST)
private List<Book> books = new ArrayList<Book>();
…
}
现在创建新的Author和几个关联的Book实体时,只需保留Author实体。
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Author a = new Author();
a.setFirstName(“John”);
a.setLastName(“Doe”);
Book b1 = new Book();
b1.setTitle(“John’s first book”);
a.getBooks().add(b1);
Book b2 = new Book();
b2.setTitle(“John’s second book”);
a.getBooks().add(b2);
em.persist(a);
em.getTransaction().commit();
em.close();
正如您在日志输出中看到的那样,Hibernate将操作级联到关联的Book实体,并将其持久化。
15:44:28,140 DEBUG [org.hibernate.SQL] – insert into Author (firstName, lastName, version, id) values (?, ?, ?, ?)
15:44:28,147 DEBUG [org.hibernate.SQL] – insert into Book (publisherid, publishingDate, title, version, id) values (?, ?, ?, ?, ?)
15:44:28,150 DEBUG [org.hibernate.SQL] – insert into Book (publisherid, publishingDate, title, version, id) values (?, ?, ?, ?, ?)