Reputation: 4615
I want to model a sample m:n relationship between the entities 'order' and 'items'
The corresponding mapping sections are:
Order.java:
...
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="ORDER_ID")
private Long orderId;
@Column(name="ORDER_DESCRIPTION")
private String description;
@JoinTable(name ="ORDER_ITEMS",
joinColumns=@JoinColumn(name="ORDER_ID", referencedColumnName="ORDER_ID"),
inverseJoinColumns=@JoinColumn(name="ITEM_ID", referencedColumnName="ITEM_ID"))
private Set<Item> items;
...
Item.java:
...
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ITEM_ID")
private Long itemId;
@Column(name = "ITEM_DESCRIPTION")
private String description;
@Column(name = "ITEM_PRICE")
private Long price;
@ManyToMany(mappedBy="items")
private Set<Order> orders;
...
Do you know why I am not able to inversely get the orders referenced with a specific item? The part @ManyToMany(mappedBy="items") gives me an "invalid mapping type for this relationship". I thought I could just create arbitrary items and orders, add the items to the orders, and get subsequently all the orders regarding to an item.
In my case, an item.getOrders() gives me a NullPointerException.
Thanks a lot!
Upvotes: 0
Views: 1620
Reputation: 691983
Simply because you forgot to add the ManyToMany annotation on items:
@ManyToMany
@JoinTable(name ="ORDER_ITEMS",
joinColumns=@JoinColumn(name="ORDER_ID", referencedColumnName="ORDER_ID"),
inverseJoinColumns=@JoinColumn(name="ITEM_ID", referencedColumnName="ITEM_ID"))
private Set<Item> items;
Upvotes: 2