Spring Boot JPA one-to-many bidirectional foreign key is null

Viewed 34

I have two entities Account and Order where one account can have one or more orders. I want to be able to log into an Account and then have order(s) for that account. Whenever I post an order for an existing account everything gets filled in properly but "account_id"(the foreign key). I would like to know why "account_id" is null and what I can do to address this problem, thanks.

Here is the Account entity:

@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 */

    

Here is the Order entity:

@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 */

Here is the Order Repository

public interface OrderRepository extends JpaRepository<Order, Long> {
    Page<Order> findByAccountId(@Param("id") Long id, Pageable pageable);
}

Here is the Order Controller

@RestController
@RequestMapping("/api")
public class OrderController {

    @Autowired
    private OrderRepository orderRepository;

    @PostMapping("/save-order")
    public Order saveOrder(@RequestBody Order order) {

        return orderRepository.save(order);

    }
}

Here is the postman post for order

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 Answers

You have :

@ManyToOne
@JoinColumn(name = "account_id",nullable = false, insertable = false, updatable = false)
private Account account;

@Column(name = "account_id")
private long accountId;

Both fields account & accountId have the same name, which souldn't happen I guess.

Related