gpt4 book ai didi

java - 如何在 Spring Data JPA 或 hibernate 中存储实体

转载 作者:行者123 更新时间:2023-11-29 21:47:20 25 4
gpt4 key购买 nike

我有这 2 个实体,它们使用 OnetoMany 映射相互关联。

实体:

@Table(name = "table_A")
public class UserSum{

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false, unique = true)
private Long id;
private String order_id;
private Date order_date;
@Column(name="email_address")
private String emailAddress;

@OneToMany(mappedBy="userOrderID",fetch=FetchType.EAGER, cascade = CascadeType.ALL)
private Collection<OrderSummary> orderSummary;
// Getter and Setters

}


@Entity
@Table(name = "table_B")
public class AlphaSumm {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false, unique = true)
private Long id;
@Column(name="user_orders_id",nullable = true)
private Long userSumID;
@Column(name="total_price")
private String totalPrice;
// Getter and setters

}

要求:我想保存 UserSum 和 AphaSum。

问题:

我正在尝试这样的事情

private void save(Order order){
UserSum usersum = new UserSum();
usersum.setOrder_id(order.getOrder_id());
usersum.setOrderSummary(getAllOrdersSummary(order,userOrders));
userOrdersRepository.save(userOrders);
}

private OrderSummary processOrderSummary(Item item,UserOrders userOrders){
OrderSummary orderSummary= new OrderSummary();

// Some code here
orderSummary.setUserOrderID(userOrders.getId());
return orderSummary;
}

注意:UserOrder 是实体

但是我在数据库中得到userOrderID null。

我的理解是:我在这里明确设置 idorderSummary.setUserOrderID(userOrders.getId());

但这里它为空,因为 JPA/Hibernate 一旦保存数据就会创建 id,所以我如何映射它们。我想到的一个解决方案是这样的

userOrders = userOrdersRepository.save(userOrders);  // to get the id and then perform rest of the operation.
userOrders.setOrderSummary(getAllOrdersSummary(order,userOrders));
userOrdersRepository.save(userOrders);

但我很确定这不是正确的方法。请向我提供您的意见,如果我缺少某些内容,请告诉我。

已更新

private List<OrderSummary> getAllOrdersSummary(Order order,UserOrders userOrders ){
List<OrderSummary> orderSummary=new ArrayList<OrderSummary>();
//some logic
orderSummary.add(processOrderSummary(item,userOrders));
return orderSummary;
}

private OrderSum processOrderSum(Item item,UserSum usersum){
OrderSum orderSum= new OrderSum();
orderSum.setOrderItems(item.getName());
orderSum.setTotalPrice(NumberFormating.currencyFormat(item.getPrice()));
orderSum.setQuantity(NumberFormating.numberFormat(item.getQty()));
orderSum.setUserOrderID(usersum.getId());

return orderSum

最佳答案

我认为您需要像这样更改 OrderSummary 实体:

@Table(name = "table_B")
public class OrderSummary {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false, unique = true)
private Long id;

@ManyToOne(fetch = FetchType.Lazy)
@JoinColumn(name="id")
private UserOrders userOrders;

@Column(name="total_price")
private String totalPrice;

}

试试吧。

请看这里:pa-joincolumn-vs-mappedby

关于java - 如何在 Spring Data JPA 或 hibernate 中存储实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34001948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com