如何在SpringBoot中创建新的SQL行并将元素添加到其列表字段中?

huangapple go评论82阅读模式
英文:

How to create a new SQL row and add elements to its list field in SpringBoot?

问题

我正在使用SpringBoot和Hibernate来管理我的Java数据库,并使用SQL。数据库中有两个表和相应的实体,分别是Order和Package。一个订单可以有多个包裹,而一个包裹只能属于一个订单,所以这是一对多的关系(Order->Package)。

因此,在Order类中有一个名为List pckList的字段。

在Package类中有一个名为Order order的字段。

现在,我从用户那里接收到一个名为newPackageList的包裹列表。我需要创建一个全新的订单,然后将所有给定的包裹添加到这个新创建的订单中。
我尝试像这样做:

Order order = new Order();
orderRepo.save(order);
Long lastId = orderRepo.lastAddedId; // 新添加的订单的ID是自动生成的。现在假设我可以获取它
order.setId(lastId);
for (Package pck : newPackageList){
   pck.setOrder(order);
   packageRepo.save(pck);
}
packageRepo.setPackageListForOrder(lastId, newPackageList); // 设置新创建的订单的pckList属性

请注意,这只是一个示例代码,您需要根据您的实际情况进行适当的调整和错误处理。

英文:

I am using SpringBoot and Hibernate in Java, and SQL to manage my database. There are two tables and corresponding entities in the database called Order and Package. One order can have many packages. And one package can be only in one order. So it's OneToMany (Order->Package).

So in Order class there is a field called List<Package> pckList.

In Package class there is a field called Order order.

Now, I receive a list newPackageList of packages from the user. I need to create a brand new order and then add all the given packages into this newly created order.
I tried doing something like this:

Order order = new Order;
orderRepo.save(order);
Long lastId = orderRepo.lastAddedId; // id of the newly added order it&#39;s auto generated. Let&#39;s assume for now I can get it
order.setId(lastId);
for (Package pck : newPackageList){
   pck.setOrder(order);
   packageRepo.save(pck);
}
packageRepo.setPackageListForOrder(lastId, newPackageList); // set the pckList property of the newly created order

答案1

得分: 1

首先,你应该审查你的实体设计。

我相当肯定你不需要在每个包裹内部使用订单。是的,也许你需要将 orderId 作为外键,而不是订单。

根据我的实践,这是导致循环 SQL 查询的原因 ---> 你选择订单 --> 订单选择包裹 --> 每个包裹再次选择订单,如此往复。为了解决这个行为,Hibernate 有一些技巧,但最好的方法是不要这样做。

所以我建议你:

@Entity
@Table(name="orders")
public class Order {

  @Id
  private int id;

  private String name;

  @OneToMany(cascade = CascadeType.PERSIST) // CascadeType.PERSIST 是必需的,以便在保存实体时保存(或更新)其集合
  @JoinColumn(name="order_id")
  private List<Package> packages = new ArrayList<>();

   // getters setters

}

@Entity
@Table(name="packages")
public class Package {

  @Id
  private int id;

  private String packageName;

  private int orderId; // 订单外键,而不是实体

  // getters setters

}

要保存数据,你需要:

Order order = new Order;

order.setPackages(newPackageList)

orderRepo.save(order); // 从 JPA CrudRepopository 中的方法
英文:

First of all you should review you entities design.
I'm pretty sure you don't need order inside your each package as entity.

Yes,may be you need orderId as a foreign key, but not order.

In my practice that was a reason for cyclic SQL selects --->
you select order -- > order select packages --> each package select order again and so on. For resolve this behavior Hibernate has some hacks but just don't)

So I suggest you:

@Entity
@Table(name=&quot;orders&quot;)
public class Order {

  @Id
  private int id;

  private String name;

  @OneToMany(cascade = CascadeType.PERSIST) // CascadeType.PERSIST is needed so that when saving an entity, its collection is saved (or updated)
  @JoinColumn(name=&quot;order_id&quot;)
  private List&lt;Package&gt; packages = new ArrayList&lt;&gt;();

   // getters setters

}

@Entity
@Table(name=&quot;packages&quot;)
public class Package {

  @Id
  private int id;

  private String packageName;

  private int orderId; // order foreign key instead of entity

  // getters setters

}

To save, you need to:

Order order = new Order;

order.setPackages(newPackageList)

orderRepo.save(order); // method from JPA CrudRepoposiry

答案2

得分: 0

orderRepo.save(order) 将返回持久化后的对象给您。您应重新分配 order 变量以指向返回的实例:

Order order = new Order();
order = orderRepo.save(order);
// 无需记住 id 或设置它
for (Package pck : newPackageList){
   pck.setOrder(order);
   packageRepo.save(pck);
}
packageRepo.setPackageListForOrder(lastId, newPackageList); // 设置新创建订单的 pckList 属性
英文:

orderRepo.save(order) will return the persisted object back to you. You should reassign the order variable to point to the returned instance:

Order order = new Order();
order = orderRepo.save(order);
// no need to remember the id or set it
for (Package pck : newPackageList){
   pck.setOrder(order);
   packageRepo.save(pck);
}
packageRepo.setPackageListForOrder(lastId, newPackageList); // set the pckList property of the newly created order

huangapple
  • 本文由 发表于 2020年8月6日 20:29:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63283676.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定