简体   繁体   English

我如何使用 Spring 注释将 map cart-cartItem-item 实体与 JPA 结合使用?

[英]How do I map cart-cartItem-item entities with JPA using Spring annotations?

I'm a beginner at JPA and table relationships and I've watched a few tutorials and consider the below to be correct(?).我是 JPA 和表关系的初学者,我看过一些教程并认为以下内容是正确的(?)。 But I'd appreciate if a more experienced eye took a look at the below if it seems right.但是,如果看起来正确的话,如果更有经验的人看看下面的内容,我将不胜感激。

I have a Cart, CartItem and an Item class. A Cart can have many CartItems.我有一个购物车、CartItem 和一个商品 class。一个购物车可以有很多 CartItem。 A CartItem can have one Item and also is part of one Cart.一个 CartItem 可以有一个 Item,也可以是一个 Cart 的一部分。 An Item can be in many CartItems.一个项目可以在许多 CartItems 中。

I'm trying to get the relationship between the tables right but I would appreciate some help since it doesn't seem to work.我正在尝试正确处理表之间的关系,但我希望得到一些帮助,因为它似乎不起作用。 I'm getting an error Cannot invoke "java.util.List.iterator()" because the return value of "...Cart.getItems()" is null. I'm assuming it's because I've set up the relationship of my tables incorrectly?我收到错误无法调用“java.util.List.iterator()”,因为“...Cart.getItems()”的返回值是 null。我假设这是因为我已经建立了关系我的表不正确?

Cart:大车:

@Entity
@Table(name="my_cart")
public class Cart {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="cart_id")
    private String cartId;

    @OneToMany(targetEntity = CartItem.class, cascade = CascadeType.ALL)
    @JoinColumn(name="cart_item_foreign_key")
    @Column(name="cart_items")
    private List<CartItem> cartItems;

    @Column(name="cart_total_number_of_items")
    private long totalNumberOfItems;
    @Column(name="cart_total_price")
    private double totalPrice;

// + getters and setters and constructor

CartItem:购物车:

@Entity
@Table(name="cart_items")
public class CartItem {
    @Id
    @Column(name="cartitem_id")
    private String itemId;
    @Column(name="cart_item_name")
    private String productName;
    @Column(name="cart_item_description")
    private String itemDescription;
    @Column(name="cart_item_quantity")
    private int itemQuantity;
    @Column(name="cart_item_price")
    private double itemPrice;

    @ManyToOne
    Cart cart;

    @OneToOne
    private Item product;

Item:物品:

@Entity
@Table(name="my_items")
public class Item {
    @Id
    @Column(name="item_id")
    private String itemId;
    @Column(name="item_name", nullable = false)
    private String name;
    @Column(name="item_description", nullable = false)
    private String description;
    @Column(name="item_price", nullable = false)
    private Double price;

    @OneToOne(mappedBy = "item")
    CartItem cartItem;

Could someone with more experience please point me in the right direction with the above table relationships?有更多经验的人可以用上面的表格关系为我指明正确的方向吗? Thank you谢谢

In the Cart class, when you use @JoinColumn , you can omit @Column , in @JoinColumn there is a name attribute that is the column name.在Cart class中,使用@JoinColumn时,可以省略@Column,@ JoinColumn中有一个name属性,就是列名。

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

相关问题 如何使用JPA注释映射一组字符串对象? - How do I map a set of string objects using JPA Annotations? Spring 数据 JPA:如何使用注释连接两个实体 - Spring Data JPA: How to join two entities using annotations 如何使用JPA映射同一超类的实体之间的关系? - How do I map relationships between entities of the same super class using JPA? 如何使用Hibernate / JPA注释覆盖GenerationType策略? - How do I override the GenerationType strategy using Hibernate/JPA annotations? 如何使用hibernate JPA注释映射嵌套集合Map <Key,List <Values >>? - How do I map a nested collection, Map<Key,List<Values>>, with hibernate JPA annotations? 如何在 JPA 中映射这些实体 - How to map these entities in JPA 如何使用Spring和注解加载策略图? - How can i load the strategy map using spring and annotations? 如何在JPA中映射这两个简单实体? - How should I map these 2 simple entities in JPA? 如何使用Spring Boot JPA在这两个类之间建立一对一的关系?另外,如何保存实体? - How can i establish a one to one relationship between this two classes using spring boot jpa?, also, how do i save the entities? 如何使用 Spring JPA/Hibernate 将 SINGLE 表查询的 1 列映射到 COLLECTION - How do I map 1 column of a SINGLE table query to a COLLECTION using Spring JPA/Hibernate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM