-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: @ManyToOne / only parent object saved
PostPosted: Fri Jan 27, 2006 11:36 am 
Newbie

Joined: Thu Jul 28, 2005 5:53 am
Posts: 9
Location: Poland
Hi,

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. I practically copied the example from hib-annot docs. However as I run the method below only a user is saved in the db i.e. without their places though the user has a reference to not empty places Set.


// this method is invoked in a Spring managed transaction so the session is opened and closed outside this method
public void test() {
Session currentSession = sessionFactory.getCurrentSession();
User user = new User("test");
Place place = new Place();
user.getPlaces().add(place);
currentSession.save(user);
}

The User and Place classes are very simple(I omit the methods not related to the issue):

@Entity()
@Table(name = "user")
public class User {
private Long id;
private Set<Place> places = new HashSet<Place>();

@OneToMany
public Set<Place> getPlaces() {
return places;
}

@Id @GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {
return id;
}

@Entity
@Table(name = "place")
public class Place {
private Long id;
private User user;

@Id @GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {
return id;
}

@ManyToOne
public User getUser() {
return user;
}

Could you point what is missing?

Thanks,

Maciek


Top
 Profile  
 
 Post subject: Re: @ManyToOne / only parent object saved
PostPosted: Fri Jan 27, 2006 12:30 pm 
Newbie

Joined: Mon Jan 23, 2006 1:11 pm
Posts: 15
Location: Leuven, Belgium
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.

_________________
Don't applaud, throw money (paypal to timvw@users.sourceforge.net) - http://www.timvw.be


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.