Adding to what @Pirho stated. JPA does not require duplicate mappings - in fact, some JPA providers will flag your posted code and throw warnings and exceptions outlining that it is a problem. You have setup Category and Event to both have a JoinColumn definition, so both mappings control the Event.category_id column. This should not be allowed because you get into situations where a specific Event instance can reference CategoryA, but be in CategoryB's event list. What happens in the database is then undefined, as there is only one Event.category_id foreign key. There is no concept of a dominant mapping type or class.
What might have been meant is something more like:
public class Event {
@ManyToOne
@JoinColumn(name = "category_id")
private Category category;
}
public class Category {
@OneToMany(mappedBy="category")
private List<Event> events;
}
This is known as a bidirectional relationship, as you can traverse it from both sides in the object model. In the database though, there is only the Event.category_id, and it is controlled/owned by the Event.category reference/relationship. If you set Event.category to a different Category instance, JPA will update the foriegn key - it is up to you to set the Category instances appropriately so that the instances in memory are insynch with the database. Otherwise, they might be stale until they get reloaded. category, use it in queries, and even merge/persist/delete
There is no need for this though. You could map it as a unidirectional relationship from either side:
public class Event {
@ManyToOne
@JoinColumn(name = "category_id")
private Category category;
}
Or from the other side if the Event.category relationship doesn't exist:
public class Category {
@OneToMany
@JoinColumn(name = "category_id")
private List<Event> events;
}
There are a few questions touching on this already