hibernate-insert-table和onetomany-joincolumn

zqdjd7g9  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(216)

我有两个表:submitorder和orderitem表submitorder包含@onetomany(orderitem)和@join column(submitorder id)。表orderitem有一个包含提交者id的字段。结果只插入提交订单表的数据,而不插入订单项目表。
不知道我搞砸了哪一部分。
提交人实体:

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

}

订单项实体:

@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

休息控制器

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

}
myzjeezk

myzjeezk1#

其中一个可能的问题与从angular发送的属性的名称有关。在实体中,“orderitem”列表的名称为“orderitem”,但发送时的名称为“cartorder”。尝试以与实体中的属性同名的Angular 发送属性。
aldo,检查属性“subtotal”是否因为有一点细节而保存在表中(Angular :“小计”与实体:“小计”)
正确的解决方案
我对你的实体类做了一个api,发现了问题。从angular发送“orderitem”的正确方法是不带下划线(orderitem),并且不需要在“orderitem”的每个元素中发送“id”。我添加了api的示例,这样您就可以下载并尝试使用postman o其他工具插入示例。api示例

相关问题