1

I am using jhipster to write a online cake shop.

It has several classes: CakeOrder, CakeOrderItems, User, Cake, CakeItem, Address. CakeOrder has one User, one Address and many CakeOrderItems.

class CakeOrder looks like this:

@Entity
@Table(name = "cake_order")
public class CakeOrder implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "order_date")
    private ZonedDateTime orderDate;

    @Column(name = "ship_date")
    private ZonedDateTime shipDate;

    @Enumerated(EnumType.STRING)
    @Column(name = "status")
    private CakeOrderStatus status;

    @OneToMany(mappedBy = "cakeOrder")
    // @JsonIgnore
    private Set<CakeOrderItem> cakeOrderItems = new HashSet<>();

    @ManyToOne
    @JoinColumn(name = "address_id")
    private Address address;

    @ManyToOne
    @JoinColumn(name = "user_id")
    private User user;
    // ...
}

Everything works fine but the createCakeOrder. Here is the code.

/**
 * POST  /cakeOrders -> Create a new cakeOrder.
 */
@RequestMapping(value = "/cakeOrders",
    method = RequestMethod.POST,
    produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
//
public ResponseEntity<CakeOrder> createCakeOrder(@RequestBody CakeOrder cakeOrder) throws URISyntaxException {
    if (cakeOrder.getId() != null) {
        return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("cakeOrder", "idexists", "A new cakeOrder cannot already have an ID")).body(null);
    }
    // TODO: use transaction
    // Create CakeOrder
    CakeOrder result = cakeOrderRepository.save(cakeOrder);
    // Create CakeOrderItems belonging to that CakeOrder
    for(CakeOrderItem item : cakeOrder.getCakeOrderItems()){
        item.setCakeOrder(order);
        cakeOrderItemRepository.save(item);

    }
    return ResponseEntity.created(new URI("/api/cakeOrders/" + result.getId()))
        .headers(HeaderUtil.createEntityCreationAlert("cakeOrder", result.getId().toString()))
        .body(result);
}

I post the following Json object as an order to that controller.

Here is the order object:

{
    "address": {
        "id": 1
    },
    "user": {
        "id": 5
    },
    "cakeOrderItems": [
        {
            "cakeItem": {
                "id": 4
            },
            "unitPrice": 268,
            "quantity": 1
        },
        {
            "cakeItem": {
                "id": 10
            },
            "unitPrice": 175,
            "quantity": 1
        },
        {
            "cakeItem": {
                "id": 18
            },
            "unitPrice": 58,
            "quantity": 1
        }
    ]
}

Well, here is the problem. In the controller, cakeOrder gets only one CakeOrderItem while there are three in the json object.

Is it a bug or something like misconfiguration? How can I get it work?

charlietfl
  • 170,828
  • 13
  • 121
  • 150

1 Answers1

4

For the CakeOrderItem class you might want to implement equals and hashcode methods. You are using a Set so when you are inserting data, the implementation of HashSet will check if there are any other equal object inside already. If yes, new data will not be inserted.

Check this answer regarding what happens behind a framework (with lazy loading and proxy objects): What issues should be considered when overriding equals and hashCode in Java?

You also have more details about equals and hashcode there.

Community
  • 1
  • 1
mkbrv
  • 407
  • 4
  • 13
  • 1
    Thanks very much. I have changed the `equals` and `hashCode` methods. and it works. The entity classes generated by jhipster, like CakeOrderItem use their ids to compare and hash. The elements in that json array do not have id assigned ( should be assigned by the underlying database when inserted), so the corresponding java objects' ids are null and they equal to each other. So cakeOrderItems set will have only the the first element of that array added. – Yangyang Zhang Jan 19 '16 at 21:12