我有两个实体Account和Order,其中一个帐户可以有一个或多个订单。我希望能够登录到一个帐户,然后有该帐户的订单。每当我发布一个现有帐户的订单时,除了“account_id”(外键)之外,所有内容都正确填写。我想知道为什么“account_id”为空,以及我可以做些什么来解决这个问题,谢谢。
以下是帐户实体:
@Entity
@Table(name="account")
public class Account {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private long id;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
@Column(name="email")
private String email;
@Column(name="user_name")
private String userName;
@Column(name="password")
private String password;
@Column(name="address")
private String address;
@Column(name="city")
private String city;
@Column(name="state")
private String state;
@Column(name="country")
private String country;
@Column(name="zipcode")
private String zipcode;
@Column(name="credit_card_number")
private String creditCardNumber;
@Column(name="credit_card_code")
private int creditCardCode;
@Column(name="credit_card_name")
private String creditCardName;
@Column(name="credit_card_expiration_month")
private int creditCardExpirationMonth;
@Column(name="credit_card_expiration_year")
private int creditCardExpirationYear;
@OneToMany(mappedBy = "account", cascade = CascadeType.ALL)
private List<Order> orders;
/* constructor */
/* Getters and setters */
下面是订单实体:
@Entity
@Table(name="orders")
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private long id;
@Column(name="total_price")
private int totalPrice;
@Column(name="total_quantity")
private int totalQuantity;
@Column(name="date_created")
@CreationTimestamp
private Date dateCreated;
@Column(name="shipping_address")
private String shippingAddress;
@ManyToOne
@JoinColumn(name = "account_id",nullable = false, insertable = false, updatable = false)
private Account account;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "order")
private Set<OrderItem> orderItems = new HashSet<>();
/* constructor */
/* Getters and setters */
这里是订单存储库
public interface OrderRepository extends JpaRepository<Order, Long> {
Page<Order> findByAccountId(@Param("id") Long id, Pageable pageable);
}
这里是订单管理员
@RestController
@RequestMapping("/api")
public class OrderController {
@Autowired
private OrderRepository orderRepository;
@PostMapping("/save-order")
public Order saveOrder(@RequestBody Order order) {
return orderRepository.save(order);
}
}
这是 Postman 的邮件,请查收
URL: "http://localhost:8080/api/save-order"
{
"totalPrice": 10.17,
"totalQuantity": 3,
"dateCreated": "2022-09-05",
"shippingAddress": "123 Road, London, United Kingdom",
"account": [{
"id": 1,
"firstName": "Winston",
"lastName": "Smith",
"email": "smith22@gmail.com",
"userName": "wilsonsmith22",
"password": "mypassword22",
"address": "123 Road",
"city": "London",
"state": "London Region",
"country": "United Kingdom",
"zipcode": "220220",
"creditCardNumber": "4422336644885500",
"creditCardCode": 234,
"creditCardName": "Winston Smith",
"creditCardExpirationMonth": 5,
"creditCardExpirationYear": 2025
}]
}
1条答案
按热度按时间nafvub8i1#
您拥有:
两个字段
account
和accountId
有相同的名称,我想这不可能发生。