简体   繁体   English

Hibernate 使用 OneToMany JoinColumn 插入表

[英]Hibernate insert table with OneToMany JoinColumn

I have two table: SubmitOrder and OrderItem Table SubmitOrder contain @OneToMany(OrderItem) and @Join Column(submitOrder id).我有两个表:SubmitOrder 和 OrderItem 表 SubmitOrder 包含 @OneToMany(OrderItem) 和 @Join Column(submitOrder id)。 Table OrderItem have a field contain SubmitOrder id.表 OrderItem 有一个字段包含 SubmitOrder id。 The result only insert the data to submit_order table but not order_item table.结果只将数据插入到 submit_order 表中,而不是 order_item 表中。

Not sure which part I mess up.不知道我搞砸了哪一部分。

SubmitOrder Entity:提交订单实体:

@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:订单项实体:

@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;
    }
}

the data that pass from angular:从 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 Controller REST Controller

@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存储库

@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);
    }

}

One of the possible problems is related to the name of the attributes that you send from Angular.其中一个可能的问题与您从 Angular 发送的属性名称有关。 In the entity, the list of "OrderItem" has the name "orderItem" but Angular sends with the name "cartOrder".在实体中,“OrderItem”列表的名称为“orderItem”,但 Angular 发送名称为“cartOrder”。 Try to send the attributes in Angular with the same name as the attributes in the Entity.尝试将Angular中的属性与Entity中的属性同名发送。

Aldo, check if attribute "subtotal" was save in the table because there is a little detail.奥尔多,检查属性“小计”是否保存在表中,因为有一些细节。 (Angular: "subTotal" vs Entity: "subtotal") (角度:“小计”与实体:“小计”)

Correct solution正确的解决方案

I did an API with your entity classes and I found the problem.我用你的实体类做了一个 API,我发现了问题。 The correct way to send the "orderItem" from Angular is without underscore (orderItem) and you need to not send the "id" in each element of "orderItem".从 Angular 发送“orderItem”的正确方法是没有下划线(orderItem),并且您不需要在“orderItem”的每个元素中发送“id”。 I add the example of the API so you can download and try to insert with the example using Postman o some other tool.我添加了 API 的示例,因此您可以下载并尝试使用 Postman 或其他工具插入示例。 API Example API 示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM