【问题标题】:Hibernate insert table with OneToMany JoinColumn带有 OneToMany JoinColumn 的休眠插入表
【发布时间】:2020-12-28 20:06:57
【问题描述】:

我有两个表:SubmitOrder 和 OrderItem 表 SubmitOrder 包含 @OneToMany(OrderItem) 和 @Join Column(submitOrder id)。 表 OrderItem 有一个字段包含 SubmitOrder id。 结果只向 submit_order 表插入数据,而没有向 order_item 表插入数据。

不确定我搞砸了哪一部分。

提交订单实体:

@Entity
@Table(name="submit_order")
public class SubmitOrder {
    
    // Define Fields
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private int id;
    
    @Column(name="name")
    private String name;
    
    @Column(name="email")
    private String email;
    
    @Column(name="phone")
    private String phone;
    
    @Column(name="pickup")
    private String pickup;
    
    @Column(name="subtotal")
    private double subtotal;
    
    @Column(name="tax")
    private double tax;
    
    @Column(name="total")
    private double total;
    
    @OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
    @JoinColumn(name="order_id")
    private List<OrderItem> orderItem;
    
    public SubmitOrder() {}

    public SubmitOrder(String name, String email, String phone, String pickup, double subtotal, double tax, double total) {
        this.name = name;
        this.email = email;
        this.phone = phone;
        this.pickup = pickup;
        this.subtotal = subtotal;
        this.tax = tax;
        this.total = total;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPickup() {
        return pickup;
    }

    public void setPickup(String pickup) {
        this.pickup = pickup;
    }

    public double getSubtotal() {
        return subtotal;
    }

    public void setSubtotal(double subtotal) {
        this.subtotal = subtotal;
    }

    public double getTax() {
        return tax;
    }

    public void setTax(double tax) {
        this.tax = tax;
    }

    public double getTotal() {
        return total;
    }

    public void setTotal(double total) {
        this.total = total;
    }

    public List<OrderItem> getOrderItem() {
        return orderItem;
    }

    public void setOrderItem(List<OrderItem> orderItem) {
        this.orderItem = orderItem;
    }

}

OrderItem 实体:

@Entity
@Table(name="order_item")
public class OrderItem {

    // Define Fields
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private int id;
    
    @Column(name="name")
    private String name;
    
    @Column(name="section")
    private String section;
    
    @Column(name="size")
    private String size;
    
    @Column(name="quantity")
    private int quantity;
    
    @Column(name="price")
    private double price;
        
    public OrderItem() {}

    public OrderItem(String name, String section, String size, int quantity, double price) {
        this.name = name;
        this.section = section;
        this.size = size;
        this.quantity = quantity;
        this.price = price;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSection() {
        return section;
    }

    public void setSection(String section) {
        this.section = section;
    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}

从 angular 传过来的数据:

SubmitOrder {name: "Sam", email: "sam@email.com", phone: "221-442-3542", subTotal: 73.32, tax: 5.8656, …}
cartOrder: Array(8)
0: {id: 24, section: "Lo Mein", name: "Beef Lo Mein", size: "Small", price: 6.6, …}
1: {id: 29, section: "Dinner", name: "Peper steak with Onion", size: "Dinner", price: 8.46, …}
2: {id: 30, section: "Appetizers", name: "Vegetable Spring Roll (2 pc.)", size: "None", price: 2.42, …}
3: {id: 35, section: "Appetizers", name: "Vegetable Egg Roll (1 pc.)", size: "None", price: 1.32, …}
4: {id: 36, section: "Appetizers", name: "Roast Port Egg Roll (1 pc.)", size: "None", price: 1.32, …}
5: {id: 38, section: "Soup", name: "Wonton Soup", size: "Small", price: 2.42, …}
6: {id: 39, section: "Soup", name: "Egg Drop Soup", size: "Large", price: 3.2, …}
7: {id: 40, section: "Appetizers", name: "Chinese Donuts (10 pc.)", size: "None", price: 4.24, …}
length: 8
__proto__: Array(0)
email: "sam@email.com"
name: "Sam"
phone: "221-442-3542"
pickup: "ASAP"
subTotal: 73.32
tax: 5.8656
total: 79.1856
__proto__: Object

REST 控制器

@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "http://localhost:4200")
public class SubmitOrderRESTController {
    private SubmitOrderService submitOrderService;

    @Autowired
    public SubmitOrderRESTController(SubmitOrderService theSubmitOrderService) {
        submitOrderService = theSubmitOrderService;
    }
    
    @PostMapping("/submit")
    public SubmitOrder saveOrder(@RequestBody SubmitOrder theSubmitOrder) {
        theSubmitOrder.setId(0);
        submitOrderService.save(theSubmitOrder);
        return theSubmitOrder;
    }
    
}

存储库

@Repository
public class SubmitOrderDAOImplement implements SubmitOrderDAO {
    // Define field for entitymanager
    private EntityManager entityManager;
    
    public SubmitOrderDAOImplement(EntityManager theEntityManager){
        entityManager = theEntityManager;
    }
    
    @Override
    public void save(SubmitOrder theSubmitOrder) {
        // Get the current hibernate session
        Session currentSession = entityManager.unwrap(Session.class);
        // Save rates
        currentSession.saveOrUpdate(theSubmitOrder);
    }

}

【问题讨论】:

  • 您是否添加了有关该问题的更多详细信息?您是否有控制器和存储库但没有插入 OrderItem?

标签: java mysql angular spring hibernate


【解决方案1】:

其中一个可能的问题与您从 Angular 发送的属性的名称有关。在实体中,“OrderItem”列表的名称为“orderItem”,但 Angular 使用名称“cartOrder”发送。尝试在 Angular 中发送与实体中的属性同名的属性。

Aldo,检查属性“subtotal”是否保存在表中,因为有一点细节。 (角度:“小计”与实体:“小计”)

正确的解决方案

我用你的实体类做了一个 API,我发现了问题。从 Angular 发送“orderItem”的正确方法是没有下划线(orderItem),并且您不需要在“orderItem”的每个元素中发送“id”。我添加了 API 示例,以便您可以下载并尝试使用 Postman 或其他工具插入示例。 API Example

【讨论】:

  • 感谢您指出小计问题,我已经解决了。但是还有一个问题,后端没有拿到orderItems列表SubmitOrder [id=0, name=Yun, email=yun@email.com, phone=124-343-3433, pickup=ASAP, subtotal=73.32, tax=5.8656, total=79.1856, orderItem=null]
  • 你是否将 Angular 中的属性名称从“cartOrder”更改为“orderItem”?
  • 是的,我确实将属性名称从“cartOrder”更改为“orderItem”
  • 以防万一,因为我没有API的所有配置。您能否检查问题是否仍然存在,将“orderItem”更改为“order_item”?因为你可以配置 Spring Boot 将下划线转换为小写名称。
  • 用 Postman 测试一下就行了!非常感谢你的帮助。将重构我的前端代码。
猜你喜欢
  • 2015-07-27
  • 1970-01-01
  • 2017-02-13
  • 2013-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-21
相关资源
最近更新 更多