Reputation: 1381
How to create enteties in Ebean that reference automaticly with other tabels?
For example in this case:
Code:
@Entity
public class User extends Model {
@Id
public int id;
@OneToMany
public List<PhotoSerie> photoSeries;
//XXXX
}
@Entity
public class PhotoSerie extends Model {
@Id
public int id;
@OneToMany
public List<Photo> photo;
//XXXX
}
@Entity
public class Photo extends Model {
@Id
public int id;
@OneToOne
PhotoExtension photoExtension;
//XXXX
}
@Entity
public class PhotoExtension extends Model {
@Id
public int id;
public String extension;
//XXXX
}
The following error will be generated:
[error] c.a.e.s.d.BeanDescriptorManager - Error in deployment
javax.persistence.PersistenceException: Error on models.PhotoSerie.photo. @OneTo
Many MUST have Cascade.PERSIST or Cascade.ALL bejavax.persistence.PersistenceExc
eption: Error on models.PhotoSerie.photo. @OneToMany MUST have Cascade.PERSIST o
r Cascade.ALL because this is a unidirectional relationship. That is, there is n
o property of type class models.PhotoSerie on class models.Photocause this is a
unidirectional relationship. That is, there is no property of type class models.
PhotoSerie on class models.Photo
How can I create entities that join in a correct way with other tables?
Upvotes: 1
Views: 937
Reputation: 2465
You need the mappedBy on your OneToMany annotations, and CascadeType.ALL on the owner of the relationship. EBean is a little different that regular ORM, and in the play docs, they mention to use setter/getter instead of field reference. Also you need some finders to do lookups. So using setter/getter approach, with play 2.1.2 (at least what I tested with) this works:
User:
package models.test;
import play.db.ebean.Model;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
public class User extends Model {
@Id
public int id;
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
public List<PhotoSeries> photoSeries;
public static Finder<String, User> find = new Finder<String, User>(
String.class, User.class);
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public List<PhotoSeries> getPhotoSeries() {
return photoSeries;
}
public void setPhotoSeries(List<PhotoSeries> photoSeries) {
this.photoSeries = photoSeries;
}
}
PhotoSeries:
package models.test;
import play.db.ebean.Model;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
public class PhotoSeries extends Model {
@Id
public int id;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "photoSeries")
public List<Photo> photoList;
@ManyToOne
public User user;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public List<Photo> getPhotoList() {
return photoList;
}
public void setPhotoList(List<Photo> photoList) {
this.photoList = photoList;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
PhotoExtension:
package models.test;
import play.db.ebean.Model;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class PhotoExtension extends Model {
@Id
public int id;
public String extension;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getExtension() {
return extension;
}
public void setExtension(String extension) {
this.extension = extension;
}
}
Photo:
package models.test;
import play.db.ebean.Model;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
@Entity
public class Photo extends Model {
@Id
public int id;
@OneToOne
PhotoExtension photoExtension;
@ManyToOne
PhotoSeries photoSeries;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public PhotoExtension getPhotoExtension() {
return photoExtension;
}
public void setPhotoExtension(PhotoExtension photoExtension) {
this.photoExtension = photoExtension;
}
public PhotoSeries getPhotoSeries() {
return photoSeries;
}
public void setPhotoSeries(PhotoSeries photoSeries) {
this.photoSeries = photoSeries;
}
}
@Test
public void testPhotoSeries() {
User user = new User();
user.setName("Bob");
List<PhotoSeries> photoSeriesList = new ArrayList<PhotoSeries>();
PhotoSeries photoSeries = new PhotoSeries();
photoSeries.setUser(user);
photoSeriesList.add(photoSeries);
user.setPhotoSeries(photoSeriesList);
user.save();
user = User.find.where().eq("name", "Bob").findUnique();
log.info("user has " + user.getPhotoSeries().size() + " photo series");
List<Photo> photoList = new ArrayList<Photo>();
Photo photo = new Photo();
photoList.add(photo);
photoSeries = user.getPhotoSeries().get(0);
photoSeries.setPhotoList(photoList);
user.update();
user = User.find.where().eq("name", "Bob").findUnique();
photoSeries = user.getPhotoSeries().get(0);
photo = photoSeries.getPhotoList().get(0);
log.info("photo " + photo);
}
Upvotes: 1