0

I have implemented a bidirectional OneToMany associations using hibernate annotations. Following is my code:

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

  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  @Column(name="id")
  private int id;

  @Column(name="name")
  private String name;

  @OneToMany(cascade=CascadeType.ALL, mappedBy="cart")
  private List<Item> items;
  //getters and setters
}

The following is the Item class:

@Entity
@Table(name="Item")
public class Item {

  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  @Column(name="id")
  private int id;

  @Column(name="name")
  private String name;

  @ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
  @JoinColumn(name="cart_id")
  private Cart cart;

  //getters and setters
}

The following code saves data:

public static void saveCart(Session session) {
    Transaction tx = session.beginTransaction();

    Cart c1 = new Cart();
    c1.setName("Cart 1");

    Item item1 = new Item();
    item1.setName("item1");

    Item item2 = new Item();
    item2.setName("item2");

    List<Item> items = new ArrayList<Item>();
    items.add(item1);
    items.add(item2);

    c1.setItems(items);

    session.save(c1);
    tx.commit();
}

This inserts one record in the cart table & 2 records in the items table . However the cart_id column in the item table is not updated. Any idea why?

I have implemented this based on the following example: https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate/

star67
  • 1,505
  • 7
  • 16
Revansha
  • 1,923
  • 4
  • 16
  • 21

2 Answers2

2

As Vlad Mihalcea explains in his great article: ...you need to synchronize both end of the bidirectional association.

For your case before session.save(c1); you should add:

item1.setCart(c1);
item2.setCart(c1);

Or, it's even better to create helper methods in your Cart class, such as:

public void addItem(Item item) {
    items.add(item);
    item.setCart(this);
}

public void removeItem(Item item) {
    items.remove(item);
    item.setCart(null);
}
star67
  • 1,505
  • 7
  • 16
  • I have a related question at https://stackoverflow.com/questions/52809395/bidirectional-onetomany-without-using-mappedby. Would it be possible for you to please take a look at it? Your help is much appreciated.. Thanks a lot – Revansha Oct 15 '18 at 03:31
0

In your Cart entity, the column name for id must be cart_id, because in your Item class you are using it for JoinColumn. They both should have the same name.

Fred A
  • 116
  • 1
  • 6
  • no, that's not the case =) here it is - https://stackoverflow.com/a/52805306/8189903 actually it's not required to have foreign key being named the same way as primary key it references – star67 Oct 14 '18 at 18:12
  • You are right somehow, but the problem is that if you have different names, then you must mention referencedColumnName. Example: @JoinColumn(name = "differentName", referencedColumnName = "exactColumnName"). So, you must somehow mention the exact column name you want to join. – Fred A Oct 14 '18 at 18:21
  • You can also read this. https://stackoverflow.com/questions/11156199/one-to-many-hibernate-join-if-column-names-are-different – Fred A Oct 14 '18 at 18:22