我需要保存子表值而不检索值。
POJO班级:
public class User {
private String firstName;
@Id
@SequenceGenerator(name = "userID", sequenceName = "userID")
@GeneratedValue(strategy = GenerationType.AUTO, generator = "userID")
@Column(name = "id")
private Long id;
private String lastName;
private Integer passwordReset;
@Column(name = "useremail", nullable = false, unique = true)
@Email
@NotEmpty
private String email;
@Column(name = "password", nullable = false)
private String password;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", referencedColumnName = "id")
private List<WashingOrder> orderList;
@Column(unique = true)
@NotEmpty(message = "{username}")
private String username;
public String getFirstName() {
return firstName;
}
public Long getId() {
return id;
}
public String getLastName() {
return lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public String getUsername() {
return username;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setId(Long id) {
this.id = id;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setPassword(String password) {
this.password = password;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getPasswordReset() {
return passwordReset;
}
public void setPasswordReset(Integer passwordReset) {
this.passwordReset = passwordReset;
}
public List<WashingOrder> getOrderList() {
return orderList;
}
public void setOrderList(List<WashingOrder> orderList) {
this.orderList = orderList;
}
}
public class WashingOrder {
@Id
@SequenceGenerator(name = "orderNo", sequenceName = "orderNo")
@GeneratedValue(strategy = GenerationType.AUTO, generator = "orderNo")
@Column(name = "orderNo")
private Long orderNo;
@Column(name = "totalClothes", nullable = false)
@NotNull
private Integer totalClothes;
@Column(name = "mensCloth", nullable = true)
private Integer mensCloth;
@Column(name = "womensCloth", nullable = true)
private Integer womensCloth;
@Column(name = "otherCloth", nullable = true)
private Integer otherClothes;
@Column(name = "deliveryDate", nullable = true)
/* @DateTimeFormat(pattern = "yyyy-mm-dd") */
private Date deliveryDate;
@Column(name = "status", nullable = true)
private String orderStatus;
public Long getOrderNo() {
return orderNo;
}
public void setOrderNo(Long orderNo) {
this.orderNo = orderNo;
}
public Integer getTotalClothes() {
return totalClothes;
}
public void setTotalClothes(Integer totalClothes) {
this.totalClothes = totalClothes;
}
public Integer getMensCloth() {
return mensCloth;
}
public void setMensCloth(Integer mensCloth) {
this.mensCloth = mensCloth;
}
public Integer getWomensCloth() {
return womensCloth;
}
public void setWomensCloth(Integer womensCloth) {
this.womensCloth = womensCloth;
}
public Integer getOtherClothes() {
return otherClothes;
}
public void setOtherClothes(Integer otherClothes) {
this.otherClothes = otherClothes;
}
public Date getDeliveryDate() {
return deliveryDate;
}
public void setDeliveryDate(Date deliveryDate) {
this.deliveryDate = deliveryDate;
}
public String getOrderStatus() {
return orderStatus;
}
public void setOrderStatus(String orderStatus) {
this.orderStatus = orderStatus;
}
}
实现:
Session session = sessionFactory.getCurrentSession();
long id = 1;
Query query = session.createQuery("from user where id =" + id);
User emp6 = (User) session.load(User.class, new Long(1));
User user = (User) query.uniqueResult();
List<WashingOrder> washingOrder = user.getOrderList();
washingOrder.add(order);
user.setOrderList(washingOrder);
session.save(user);
Long orderID = (long) 1;
return orderID;
如果您检查我首先获得订单列表(user.getOrderList()),然后我将添加新列表并保存用户。
但我想保存用户的新订单列表,而不检索以前的订单列表。
有可能在不检索特定用户的先前订单列表的情况下添加新订单数据。
答案 0 :(得分:0)
您需要更改映射
public class User {
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<WashingOrder> orderList;
}
public class WashingOrder {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;
}
Session session = sessionFactory.getCurrentSession();
User user = (User) session.load(User.class, 1L);
WashingOrder order = new WashingOrder();
order.setUser(user);
session.save(order);
您可以尝试使用假User
而不是加载
WashingOrder order = new WashingOrder();
order.setUser(new User(1L));
session.save(order);