Reputation: 7011
I´ve got this Model on my Play application.
@Entity
@Table(name="SHOPPER")
public class User extends GenericModel {
...
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "SASHNBR", insertable = true, updatable = true)
public List<Direction> directions;
}
The Direction Model looks like this
@Entity
@Table(name="SHADDR")
public class Direccion extends GenericModel {
...
@Column(name="SASHNBR")
@Required
public Long idUser;
}
This way I´ve got an error because Direction doens't have the idUser generated when saving.
I´ve tried this way too.
@Entity
@Table(name="SHOPPER")
public class User extends GenericModel {
...
@OneToMany(cascade = CascadeType.ALL, mappedBy="user")
public List<Direction> directions;
}
The Direction Model looks like this
@Entity
@Table(name="SHADDR")
public class Direccion extends GenericModel {
...
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name = "SASHNBR", insertable = true, updatable = true)
User user;
}
But it didn't work either.
Can someone help me with this?
Thanks! :)
Upvotes: 1
Views: 2364
Reputation: 4896
You will need to save the children your self. You can look at the tutorial for an example.
public User addDirection(Direction direction) {
this.directions.add(direction);
this.save();
return this;
}
@Override
public User save(){
for (Direction dir : directions) {
dir.save()
}
super.save()
return this;
}
Upvotes: 2
Reputation: 5399
Please try something like this:
User user = new User();
// [...] call required setters of user object
for (int i=0; i<5; i++) {
Direction direction = new Direction();
direction.setUser(user);
// [...] call other required setters of direction object
user.getDirections().add(direction);
}
entitymanager.persist(user);
Upvotes: 1