作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有两个表,一个是用户,另一个是用户地址详细信息。
在这里,用户将添加新的详细信息,我需要插入到多个边表中。
这里是一对多的关系。我已向表中添加新值,但没有删除现有关系。
但就我而言,发生的情况是旧关系被删除,新关系被添加。
实体类:
洗涤顺序:
@Entity(name = "ClothDetails")
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;
}
用户信息:
@Entity(name = "user")
public class UserInformation {
private String firstName;
@Id
@SequenceGenerator(name = "userID", sequenceName = "userID")
@GeneratedValue(strategy = GenerationType.AUTO, generator = "userID")
@Column(name = "id")
private Long id;
private String lastName;
@Column(name = "reset", nullable = false)
private Integer passwordReset;
@Column(name = "useremail", nullable = false, unique = true)
@Email
@NotEmpty
private String email;
@Column(name = "password", nullable = false)
private String password;
@OneToOne(mappedBy = "user", optional = true, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "id")
private Role role;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "id")
private List<CreditCards> creditCards;
@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 Role getRole() {
return role;
}
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 setRole(Role role) {
this.role = role;
}
public void setUsername(String username) {
this.username = username;
}
public List<CreditCards> getCreditCards() {
return creditCards;
}
public void setCreditCards(List<CreditCards> creditCards) {
this.creditCards = creditCards;
}
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;
}
代码:
Session session = sessionFactory.getCurrentSession();
long id = 1;
Query query = session.createQuery("from user where id =" + id);
UserInformation emp6 = (UserInformation) session.load(UserInformation.class, new Long(1));
UserInformation user = (UserInformation) query.uniqueResult();
List<WashingOrder> washingOrder = new ArrayList<WashingOrder>();
washingOrder.add(order);
System.out.println("washingOrder" + washingOrder);
emp6.setOrderList(washingOrder);
session.saveOrUpdate(user);
Long orderID = (long) 1;
return orderID;
数据库截图:
最佳答案
当您更新“user”时,“orderList”关系也会更新,因为您已在“orderList”上设置“cascade = CascadeType.ALL”。您的代码示例将 orderList 对象替换为全新的对象。因此删除所有以前的关系,并添加一个新的关系。
如果您想保留关系并仅添加新关系,则应使用检索到的对象中的列表:
List<WashingOrder> washingOrder = emp6.getOrderList();
washingOrder.add(order);
确保预先加载“orderList”,因为您在该关系上启用了延迟加载。您可以通过以下方式执行此操作:
Hibernate.initialize(emp6.getOrderList());
关于java - 如何在一对多 hibernate 中向现有数据插入或添加新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29030630/
我是一名优秀的程序员,十分优秀!