Reputation: 1203
I create 3 model classes in playframework, and set one-one relationship and one-to many relationship in one of the class. The code snippet are as follows:
Person.java
///////////////////////
@Entity
@Table(name = "person")
public class Person extends Model{
@Id
private Long id;
private String lastName;
private String firstName;
private String userId;
private Address address;
private List<Task> tasks;
....
}
Task.java
//////////////////////////
@Entity
@Table(name = "task")
public class Task extends Model{
@Id
private Long id;
private String name;
private String descript;
private String details;
...........
}
Address.java
////////////////////
@Entity
@Table(name = "address")
public class Address extends Model{
@Id
private Long id;
private String street;
private String state;
.........
}
I create person object and set the attributes/one-one/one-many relationships. I try to save the person object with both attributes and relationships to mysql db by calling person.save().However, it ends up saving only attributes userId/firstName/LastName. the address object and tasks list object are not saved in db. My question is : is there any way to save the relationship objects in db long with person.save()? that is , after calling person.save(), address table and task table create new entires corresponding to them. I end up setting foreign keys in person class to handle this relationship manually.
Thanks in advance!
Upvotes: 0
Views: 29
Reputation: 4823
You might want to have a look at a JPA tutorial in particular topics about @OneToMany
and @OneToOne
annotations. I'd recommend https://en.wikibooks.org/wiki/Java_Persistence/OneToMany and https://en.wikibooks.org/wiki/Java_Persistence/OneToOne.
Upvotes: 1