zywno wrote:
I've got a simple relationship between a user and a place - each user can have many places and each place belongs to only one user.
In that case i would go for a foreign_key to the user in each place
Quote:
// User
@OneToMany(targetEntity=Place.class, mappedBy=“user”, cascade=CascadeType.ALL) public List getPlaces() {…}
// Place
@ManyToOne(cascade=CascadeType.PERSIST) @JoinColumn(nullable=false) public User getUser() {…}
Code:
User user = new User();
Place place1 = new Place().setUser(user);
Place place2 = new Place().setUser(user);
Vector places = new Vector(); places.add(place1); places.add(place2);
user.setPlaces(places);
em.persist(user);
Notice that places without a user set (thus null) will result in an exception (if you remove the nullable=false the place will be inserted, but the foreign-key to user will be NULL)
The other way round does work to:
Code:
User user = new User();
Place place1 = new Place().setUser(user);
em.persist(place1);
Well, this is what i concluded in
my experiments with onetomany.